簡體   English   中英

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

[英]Rspec/Rails3: NoMethodError: undefined method `text' for nil:NilClass

我有一些測試失敗了。 以下是我的測試:

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

這是輸出

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) 對訪問編輯頁面的主題控制器中的未登錄用戶進行身份驗證

 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)>'

我在這里遺漏了什么? 謝謝你的幫助!

沒有看到測試中的代碼很難知道,但有幾個潛在的原因:

1)請記住, let懶惰評估: let ,直到符號在該規范中引用不創建對象。 對於這種情況,您可能希望在visit發生之前創建user實例,但它不會存在,因為永遠不會評估user 這可能會導致身份驗證失敗。

2) subject是一個不幸的名稱選擇,因為RSpec使用subject來引用正在測試的對象實例。 這可能會也可能不會引起問題。

以備日后參考

我遇到了同樣的問題,這是因為一些手動定義的夾具違反了模型中的唯一性約束。 我猜這會導致夾具無法正確加載到測試數據庫中。

失敗的測試線是(我正在使用 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

我得到的錯誤是:

Failure/Error: expect(fixture_item).to be_valid
     
     NoMethodError:
       undefined method `text?' for nil:NilClass

通過解決這個問題(在我的例子中,通過刪除不必要的唯一性約束)這個錯誤得到了解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM