2012-08-01 57 views
2

我無法解決以下RSpec的驗證錯誤:rspec的驗證失敗:「不能爲空」

Failures: 

    1) User treating associations should have the right treatings in the right order 
    Failure/Error: FactoryGirl.create(:treating, requestor: @user, created_at: 1.day.ago) 
    ActiveRecord::RecordInvalid: 
     Validation failed: Requestee can't be blank 
    # ./spec/models/user_spec.rb:143:in `block (3 levels) in <top (required)>' 

我負責的兩個型號,用戶和處理(認爲它作爲一個會議):

user.rb

class User < ActiveRecord::Base 
    attr_accessible :name, :email, :password, :password_confirmation 
    has_secure_password 

    has_many :sent_treatings, :foreign_key => "requestor_id", :class_name => "Treating", dependent: :destroy 
    has_many :received_treatings, :foreign_key => "requestee_id", :class_name => "Treating", dependent: :destroy 

end 

treating.rb

class Treating < ActiveRecord::Base 
    attr_accessible :intro, :proposed_date, :proposed_location 

    validates :intro, presence: true, length: { maximum: 190 } 
    validates :requestor_id, presence: true 
    validates :requestee_id, presence: true 

    belongs_to :requestor, class_name: "User" 
    belongs_to :requestee, class_name: "User" 

    default_scope order: 'treatings.created_at DESC' 

end 

最後,從我的user_spec是給我這個錯誤的部分:我不知道

describe "treating associations" do 
     before { @user.save } 
     let!(:older_treating) do 
      FactoryGirl.create(:treating, requestor: @user, created_at: 1.day.ago) 
      FactoryGirl.create(:treating, requestee: @user, created_at: 1.day.ago) 

     9end 
     let!(:newer_treating) do 
      FactoryGirl.create(:treating, requestor: @user, created_at: 1.hour.ago) 
      FactoryGirl.create(:treating, requestee: @user, created_at: 1.hour.ago) 
     end 

     it "should have the right treatings in the right order" do 
      @sent_treating = requestor.sent_treatings.build(intro: "Lorem ipsum") 
      @sent_treating.requestee = requestee 

      @received_treating = requestee.received_treatings.build(intro: "Lorem ipsum") 
     @received_treating.requestor = requestor 

      requestor.sent_treatings.should == [newer_treating, older_treating] 
      requestee.received_treatings.should == [newer_treating, older_treating] 
     end 

爲什麼「被請求方」將是空白這裏可以使用一些幫助。謝謝。

編輯:工廠在下面,謝謝!

(我已經嘗試添加「用戶」,「請求」,並在下方的「前奏」「被請求方」:處理廠,但仍然沒有成功)。當我在工廠文件中添加「用戶」(以下「前奏‘Lorem存有’),我收到以下錯誤:

1) User treating associations should have the right treatings in the right order 
    Failure/Error: FactoryGirl.create(:treating, requestor: @user, created_at: 1.day.ago) 
    NoMethodError: 
     undefined method `user=' for #<Treating:0x00000104fa0668> 
    # ./spec/models/user_spec.rb:143:in `block (3 levels) in <top (required)>' 

當我添加‘請求者’和‘受邀’到工廠,而不是用戶(而除了使用),我收到以下錯誤:

Failures: 

    1) User treating associations should have the right treatings in the right order 
    Failure/Error: FactoryGirl.create(:treating, requestor: @user, created_at: 1.day.ago) 
    ArgumentError: 
     Trait not registered: requestee 
+0

你的工廠看起來像什麼? – DVG 2012-08-01 18:48:57

+0

在上面的編輯中添加了與不同工廠特性相關的工廠和錯誤。謝謝! – keypulsations 2012-08-01 19:18:09

+0

「治療」模式需要同時存在被請求者和請求者。規範中的工廠用其中一個或另一個創建對象,但不能兩者兼備。你可以發佈你用來嘗試添加請求者和請求者到處理工廠的代碼嗎?你使用「關聯」嗎?它不承認'requestee',大概是因爲實際的字段名稱是'requestee_id',並且認爲它是一個特質,這完全是另一回事。 – jordanpg 2012-08-01 20:30:59

回答

1

的問題是,你的處理廠沒有定義請求者和受邀人,另外,你需要告訴FactoryGirl該請求者和受邀人是別名爲用戶

factory :user, :aliases => [:requestor, :requestee] do 
    sequence(:name) { |n| "Person #{n}" } 
    sequence(:email) { |n| "person_#{n}@example.com"} 
    password "foobar" 
    password_confirmation "foobar" 

    factory :admin do 
     admin true 
    end 
end 

factory :treating do 
    intro "Lorem ipsum" 
    requestor 
    requestee 
end 
+0

您可能還需要在請求方和請求方關係上指定您的外鍵。 – DVG 2012-08-01 20:35:46

+0

謝謝大家,我想出了你的幫助。除了在factories.rb文件的用戶trait行中的別名中進行編碼之外,我還需要在編輯中突出顯示user_spec中較舊/較新的發送/接收處理。 – keypulsations 2012-08-01 22:41:19

1

這是修復。

factories.rb需要在所提到的別名:用戶性狀線

FactoryGirl.define do 

    factory :user, aliases: [:requestor, :requestee] do 
    sequence(:name) { |n| "Person #{n}" } 
    sequence(:email) { |n| "person_#{n}@example.com"} 
    password "foobar" 
    password_confirmation "foobar" 

    factory :admin do 
     admin true 
    end 
    end 

    factory :treating do 
    intro "Lorem ipsum" 
    requestor 
    requestee 
    end 

end 

最後,「治療協會」測試需要有舊/新的接收/發送細分爲treatings(4種變體):

describe "treating associations" do 
    before { @user.save } 
    let!(:older_sent_treating) do 
    FactoryGirl.create(:treating, requestor: @user, created_at: 1.day.ago) 
    end 
    let!(:older_received_treating) do 
    FactoryGirl.create(:treating, requestee: @user, created_at: 1.day.ago) 
    end 
    let!(:newer_sent_treating) do 
    FactoryGirl.create(:treating, requestor: @user, created_at: 1.hour.ago) 
    end 
    let!(:newer_received_treating) do 
    FactoryGirl.create(:treating, requestee: @user, created_at: 1.hour.ago) 
    end  

    it "should have the right treatings in the right order" do 
    @user.sent_treatings.should == [newer_sent_treating, older_sent_treating] 
    @user.received_treatings.should == [newer_received_treating, older_received_treating] 
    end 
end 

然後測試應該通過。謝謝!