2

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

私は2つのモデル、ユーザーと治療(会議と考えてください)を扱っています:

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

治療.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

'requestee'がここで空白になり、ヘルプを使用できる理由がわかりません。ありがとうございました。

編集:工場は下にあります、ありがとう!

FactoryGirl.define do
    factory :user 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"
  end

end

(:treatingファクトリの「intro」の下に「user」、「requestor」、および「requestee」を追加しようとしましたが、それでも成功しません)。ファクトリファイル(イントロ「Loremipsum」の下)に「user」を追加すると、次のエラーが発生します。

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

ユーザーの代わりに(そして使用に加えて)ファクトリに「requestor」と「requestee」を追加すると、次のエラーが発生します。

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
4

2 に答える 2

1

問題は、Treating Factoryにリクエスターとリクエスターが定義されていないことです。さらに、リクエスターとリクエスターがユーザーのエイリアスであることを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
于 2012-08-01T20:33:47.390 に答える
1

これが修正です。

factorys.rbには、:userトレイト行に次のように記載されているエイリアスが必要です。

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

最後に、「治療関連」テストでは、古い/新しい受信/送信された治療(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

その後、テストに合格する必要があります。ありがとう!

于 2012-08-01T22:43:41.430 に答える