我在Michael Hartl的RoR教程第8章中遇到了一个问题。由于RSpec的“其”方法为“未定义”,所以测试失败。你见过类似的东西吗?可能是什么原因?我已经检查了所有内容,与书中的内容相同...
这是我来自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
...
...
测试运行的结果说:RSpec::ExampleGroups::User::RememberToken:Class(NoMethodError)的未定义方法'its':
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 RSpec和Arguments passed to its讨论。当您使用Rspec> = 3.0时,出现了错误。因为在此版本或更高版本中,its
不是Rspec核心的一部分。您可以检查Rspec的当前One-liner syntax。
关于ruby-on-rails - RSpec的未定义方法 `its'(Hartl的Ruby on Rails教程),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28014000/