Neon_10 Neon_10 - 1 year ago 84
Ruby Question

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 = "Example User", email: "",
password: "foobar", password_confirmation: "foobar") }

subject { @user }

describe "remember token" do
before { }
its(:remember_token) { should_not be_blank }

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.................

Answer Source

You write the below as :

its(:remember_token) { should_not be_blank }


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download