Follow Along as I Stumble on the Path to Learning RoR

Testing that a Method Returns a Value with Rspec

I’m not sure if it’s because I haven’t had coffee yet today or what, but it took me awhile to think about how to test that a method returned a value.

For example, I had a method that returns either true or false.

def self.is_this_the_right_user?(user_id)

  if user_id == current_user.id

    return true

  else

    return false

  end

end

Initially in my RSpec, I had:

expect { User.is_this_the_right_user?(user_id) }.to eq true

Of course, that wasn’t correct.  Instead:

r = User.is_this_the_right_user?(user_id)

r.should eq true

Advertisements

Comments are closed.