Testing: be_new_record

53 views
Skip to first unread message

Matthias Baumgart

unread,
Jul 20, 2009, 5:36:57 AM7/20/09
to Ruby on Rails: Talk
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.

Gavin

unread,
Jul 20, 2009, 7:34:43 AM7/20/09
to Ruby on Rails: Talk
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

http://handyrailstips.com

Matthias Baumgart

unread,
Jul 21, 2009, 2:24:52 PM7/21/09
to Ruby on Rails: Talk
> 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.
Reply all
Reply to author
Forward
0 new messages