2012-08-01 56 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 

然后测试应该通过。谢谢!