ruby-on-rails – 用于RSpec的未定义方法`它'(Hartl的Ruby on Rails教程)

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 用于RSpec的未定义方法`它'(Hartl的Ruby on Rails教程)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Michael Hartl的RoR教程第8章中遇到了一个问题.测试失败,因为RSpec的“它”方法是“未定义的”.你见过类似的东西吗?可能是什么原因?我查了一切,和书中的一样……

这是来自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
...
...

测试运行的结果,它说:未定义的方法`它’为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.................

解决方法

你写下面的:
its(:remember_token) { should_not be_blank }

expect(subject.remember_token).not_to be_blank

阅读its isn’t core to RSpecArguments passed to its讨论.当你使用Rspec> = 3.0时,你就得到了错误.因为在此版本或更高版本中它不是Rspec核心的一部分.

您可以查看Rspec的当前One-liner syntax.

原文链接:https://www.f2er.com/ruby/264675.html

猜你在找的Ruby相关文章