Testing: be_new_record

Hello,

in the railscast episode 71, Ryan uses in a test be_new_record.
Running tests with be_new_record causes a NameError: undefined local
variable or method.

The episode isn’t on git and I didn’t find the code. I installed rspec
and mocha, but didn’t find on google the origin of this function. Is
this just a self implemented helper function or is it a function out
of a testing framework? Which one?

Matthias.

Rspec has a really cool feature where, if you assert that something
should be_something_else it’ll assume there’s a method called
something_else?

So for example;

Suppose you had a model called Sushi.

In your spec you could write:

describe Sushi do

it “should be awesome”
@sushi.should be_awesome
end

end

This will assume there is a method called awesome? available to @sushi
and will call it and expect it to return true.

ActiveRecord::Base, which all of your models will inherit from has a
method called “new_record?”.
By using “@model.should be_new_record” or “@model.should
be_a_new_record” you’re essentially saying that the new_record? method
should be called and should return true.

If you’re getting a NoMethod error specifically on “be_a_new_record”
then you might want to ensure you have the latest versions of rspec
and rspec-rails.

Hope that helps?

Gavin

If you’re getting a NoMethod error specifically on “be_a_new_record”
then you might want to ensure you have the latest versions of rspec
and rspec-rails.

Hmm. Actually I got this error, but I have installed rspec and rspec-
rails and put it to the config/environments/test with the following
lines:

config.gem “rspec”, :lib => false, :version => “>= 1.2.0”
config.gem “rspec-rails”, :lib => false, :version => “>= 1.2.0”
config.gem “mocha”

be_new_record is a function of rspec?

Matthias.