[英]Rspec/Rails3: NoMethodError: undefined method `text' for nil:NilClass
I've got some tests failing.我有一些测试失败了。 The following are my tests:
以下是我的测试:
require 'spec_helper'
describe "Authenticaton" do
let(:base_title){"KS > Kids | "}
subject { page }
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
let(:subject) { FactoryGirl.create(:subject) }
context "in the Subjects controller" do
describe "visiting the edit page" do
before { visit edit_subject_path(subject) }
it { should have_title("#{base_title}Home") }
end
describe "visiting the subject index" do
before { visit subjects_path }
it { should have_title("#{base_title}Home") }
end
end
end
end
And this is output这是输出
1) Authenticaton for non-signed-in users in the Subjects controller visiting the subject index 1) 对访问主题索引的主题控制器中的未登录用户进行身份验证
Failure/Error: it { should have_title("#{base_title}#{I18n.t("messages.session.sign_in")}") }
NoMethodError:
undefined method `text' for nil:NilClass
# ./spec/requests/extra_auth_pages_spec.rb:23:in `block (5 levels) in <top (required)>'
2) Authenticaton for non-signed-in users in the Subjects controller visiting the edit page 2) 对访问编辑页面的主题控制器中的未登录用户进行身份验证
Failure/Error: it { should have_title("#{base_title}Home") }
NoMethodError:
undefined method `text' for nil:NilClass
# ./spec/requests/extra_auth_pages_spec.rb:18:in `block (5 levels) in <top (required)>'
I'm missing something here?我在这里遗漏了什么? Thanks for your help!
谢谢你的帮助!
It's hard to know without seeing the code under test, but there are a couple of potential causes: 没有看到测试中的代码很难知道,但有几个潜在的原因:
1) Remember that let
is lazy-evaluated: let
does not create objects until the symbol is referenced in the spec. 1)请记住,
let
懒惰评估: let
,直到符号在该规范中引用不创建对象。 For this case, you may expect that the user
instance is created before the visit
occurs, but it won't exist because user
is never evaluated. 对于这种情况,您可能希望在
visit
发生之前创建user
实例,但它不会存在,因为永远不会评估user
。 That will likely cause an authentication failure. 这可能会导致身份验证失败。
2) subject
is an unfortunate choice of names, since RSpec uses subject
to refer to the object instance being tested. 2)
subject
是一个不幸的名称选择,因为RSpec使用subject
来引用正在测试的对象实例。 This may or may not cause a problem. 这可能会也可能不会引起问题。
I had the same problem, which was because some manually defined fixtures violated a uniqueness constraint in the model.我遇到了同样的问题,这是因为一些手动定义的夹具违反了模型中的唯一性约束。 I'm guessing this caused the fixtures to not be loaded correctly into the test database.
我猜这会导致夹具无法正确加载到测试数据库中。
The test lines which failed were (I'm using RSpec):失败的测试线是(我正在使用 RSpec):
it "should expect some value" do
fixture_item = manual_fixture_table('fixture_item_label')
expect (fixture_item).to be_valid #This line was failing
#[...]
end
The error I got was:我得到的错误是:
Failure/Error: expect(fixture_item).to be_valid
NoMethodError:
undefined method `text?' for nil:NilClass
By fixing this (in my case, by removing the uniqueness constraint which was unnecessary) this error was solved.通过解决这个问题(在我的例子中,通过删除不必要的唯一性约束)这个错误得到了解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.