Neon_10
Neon_10

Reputation: 731

undefined method `its' for RSpec (Hartl's Ruby on Rails Tutorial)

Im At Michael Hartl's RoR Tutorial Chapter 8 and I have got a problem. The test fails because of "its" method for RSpec is "undefined". Have you met something similar? what might be a reason? I have checked everything and is the same as in the book...

Here is my test code from user_spec.rb:

describe User do

 before { @user = User.new(name: "Example User", email: "[email protected]",
                password: "foobar", password_confirmation: "foobar") }

 subject { @user }

 describe "remember token" do
 before { @user.save }
 its(:remember_token) { should_not be_blank }
end
...
...

the result of tests running, it says: undefined method `its' for RSpec::ExampleGroups::User::RememberToken:Class (NoMethodError):

MBP:sample_app smi$ bundle exec rspec spec
/Users/smi/projects/sample_app/spec/models/user_spec.rb:12:in `block (2 levels) in <top (required)>': **undefined method `its' for RSpec::ExampleGroups::User::RememberToken:Class (NoMethodError)**
from /Users/smi/.rvm/gems/ruby-2.0.0-p594/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:325:in `module_exec'
from /Users/smi/.rvm/gems/ruby-2.0.0-p594/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:325:in `subclass'
from /Users/smi/.rvm/gems/ruby-2.0.0-p594/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:219:in `block in define_example_group_method'
from /Users/smi/projects/sample_app/spec/models/user_spec.rb:10:in `block in <top (required)>'
from /Users/smi/.rvm/gems/ruby-2.0.0-p594/gems/rspec-core-3.1.7/lib/rspec/core/example_group.rb:325:in `module_exec'
from /Users/smi/.rvm/g.................

Upvotes: 7

Views: 3114

Answers (2)

grek-andrian
grek-andrian

Reputation: 121

You need install gem 'rspec-its' - its provides the its method as a short-hand to specify the expected value of an attribute.

Upvotes: 6

Arup Rakshit
Arup Rakshit

Reputation: 118261

You write the below as :

its(:remember_token) { should_not be_blank }

as

expect(subject.remember_token).not_to be_blank

Read its isn't core to RSpec and Arguments passed to its discussions. As you are using Rspec >= 3.0, so you got the error. Because in this version or higher its is not the part of Rspec core.

You can check the Rspec's current One-liner syntax.

Upvotes: 16

Related Questions