Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3617014
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:34:12+00:00 2026-05-18T22:34:12+00:00

I have a simple method that I want to test with RSpec. I want

  • 0

I have a simple method that I want to test with RSpec. I want to ensure that apply decrements player.capacity by one. To do this, I have mocked a player object and am testing to see if it receives the correct messages.

Code

class DecreaseCapacity < Item
  def apply player
    player.capacity -= 1
  end
end

Test

describe DecreaseCapacity, "#apply" do
  it "should decrease capacity by one" do
    player = double()
    player.should_receive(:capacity)   # reads the capacity
    player.should_receive(:capacity=)  # decrement by one
    subject.apply player
  end
end

Failure Message

1) DecreaseCapacity#apply should decrease the player's capacity by one
   Failure/Error: subject.apply player
   undefined method `-' for nil:NilClass
   # ./item.rb:39:in `apply'
   # ./item_spec.rb:25

What’s going on here? Why is player.capacity -= 1 attempting to call - on nil?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:34:13+00:00Added an answer on May 18, 2026 at 10:34 pm

    The problem is that the way you’ve stubbed the player it will return nil when capacity is called. You need to change like so:

    player.should_receive(:capacity).and_return(0)
    player.should_receive(:capacity=).with(1)
    

    To understand why, let’s break down what happens in your code. It will be easier to see the problem if we expand the -= to:

    player.capacity = player.capacity - 1
    

    With your stubbed player it becomes this:

    player.capacity = nil - 1
    

    which is exactly what RSpec complains about.

    Now, let me suggest a better way to write the test. Your test simply mirrors your implementation, it does not test the method. What I mean by that is that it doesn’t test that the apply method increments a player’s capacity by one — it tests that apply calls capacity and then capacity=. You may think it’s the same thing, but that’s only because you know how you have implemented the method.

    This is how I would have written the test:

    it "increments a player's capacity" do
      player = Player.new # notice that I use a real Player
      player.capacity = 0
      subject.apply(player)
      player.capacity.should == 1
    end
    

    I used a real Player object instead of setting up a stub, because I assume that the implementation of Player#capacity is just an accessor, there’s no logic in there to interfere with my test. The risk with using stubs is that sometimes the stubs get even more complicated than the real objects (as in this case, I would argue), and that means that it’s more likely that your test is wrong than your actual code.

    You can also write the test like this, if you want to use the full expressiveness of RSpec:

    it "increments a player's capacity" do
      player = Player.new
      player.capacity = 0
      expect { subject.apply(player) }.to change { player.capacity }.to(1)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have a simple wcf service like this, with a test method which
i have simple interface that i want to test it out but i have'nt
I have the following JUnit test. The method that I'm testing is quite simple,
I am simply learing Ajax with jQuery and have a simple page method that
I have a simple jigsaw puzzle i'm making. I have a method that is
I have a simple mapview that has the following viewdidload method and didupdate to
So I have a very simple class that has a method called getThumbUrl() but
Fairly simple question: I have an init method on my class that has the
Say I have this simple method: public IEnumerable<uint> GetNumbers() { uint n = 0;
Hi I have this simple method which gets triggered when i click inside a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.