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

Im at Michael Hartl RoR Tutorial Chapter 8 and I have a problem. The test does not work because of "its" method for RSpec "undefined". Have you met something like this? what could be the reason? I checked everything and just like in the book ...

Here is my test code from user_spec.rb:

describe User do before { @user = User.new(name: "Example User", email: " user@example.com ", 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 running the tests, he says: the undefined `his' method 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................. 
+6
source share
2 answers

You write below:

 its(:remember_token) { should_not be_blank } 

a

 expect(subject.remember_token).not_to be_blank 

Consider its not the core for RSpec and the arguments passed into its discussion. When you use Rspec> = 3.0, you will get an error. Because in this version or higher, its not part of the Rspec kernel.

You can check the current syntax of rspec syntax.

+8
source

You need to set gem 'rspec-its' - it provides its method as short to indicate the expected value of the attribute.

+1
source

Source: https://habr.com/ru/post/981187/


All Articles