简体   繁体   English

Rspec/Rails3:NoMethodError:nil:NilClass 的未定义方法“文本”

[英]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. 这可能会也可能不会引起问题。

For the interest of future reference以备日后参考

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.

相关问题 Rails4:NoMethodError /未定义的方法&#39;*&#39;为nil:NilClass - Rails4: NoMethodError / undefined method `*' for nil:NilClass Rails-NoMethodError-未定义的方法“名称”,为nil:NilClass - Rails - NoMethodError - undefined method `name' for nil:NilClass Rails 6-NoMethodError(nil:NilClass的未定义方法“ titleize”) - Rails 6 - NoMethodError (undefined method `titleize' for nil:NilClass) Rails 4:NoMethodError:nil的未定义方法`each&#39;:NilClass - Rails 4: NoMethodError: undefined method `each' for nil:NilClass Rails DateTime-NoMethodError(nil:NilClass的未定义方法“ []”) - Rails DateTime - NoMethodError (undefined method `[]' for nil:NilClass) Ruby On Rails - NoMethodError:nil:NilClass 的未定义方法“[]” - Ruby On Rails - NoMethodError: undefined method `[]' for nil:NilClass NoMethodError:nil的未定义方法&#39;type&#39;:Rails中的NilClass - NoMethodError: Undefined method 'type' for nil:NilClass in Rails rails NoMethodError:nil:NilClass的未定义方法“” - rails NoMethodError: undefined method “ ” for nil:NilClass Rails - NoMethodError(nil:NilClass 的未定义方法 `collat​​ion&#39;) - Rails - NoMethodError (undefined method `collation' for nil:NilClass) NoMethodError,未定义的方法“名称”,为nil:NilClass -rails 3 - NoMethodError, undefined method `name' for nil:NilClass -rails 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM