2

私は Factory_Girl を開いたばかりで、依存するファクトリを構築する方法を理解しようとしています。新しいリリースのため、ほとんどの質問は時代遅れのようです。

関連付けを使用していますが、関連付けられたオブジェクトを作成する以外に、そのオブジェクトはメイン オブジェクトに関連付けられていないか、関連付けられていないようです。

基本的にここに私が持っているものがあります

factory :computer do
  serial_no "12345"
end

factory :allocation do
  association :computer_id, factory: :computer
end

割り当ては、コンピューターに属し、コンピューターには多くの割り当てがあります。基本的に、割り当ては、コンピューターが移動したときなどの記録です。

何が間違っているのかわかりませんが、これを実行するたびに、割り当ての computer_id は「1」ですが、コンピューターの ID はランダム (通常は 0 ~ 20 の数字) であり、テストは失敗します。適切なコンピューター オブジェクトが見つからないためです。

編集:十分に混乱していないかのように、実際のクラス名は Assignment です。単純にしようとしていました。実際のコードは次のとおりです。computer_id と user_id は作成時にパラメーターとして create メソッドに渡されるため、実際のコードには問題はありません。

describe "GET index" do
  it "assigns assignments as @assignment" do
    Assignment.any_instance.stubs(:valid?).returns(true)
    assignment = FactoryGirl.create :associated_assignment
    get :index, {}, valid_session
    assigns(:assignments).should eq([assignment])
  end
end

関係する工場は次のとおりです。

factory :user do
  fname "John"
  lname "Smith"
  uname "jsmith"
  position "placeholder"
end

factory :computer do
  asset_tag "12345"
  computer_name "comp1"
  make "dell"
  model "E6400"
  serial_no "abc123"
end

factory :associated_assignment, class: Assignment do
  association :user_id, factory: :user
  association :computer_id, factory: :computer
  assign_date '11-11-2008'
end

そして、コントローラーは次のとおりです。

def index
  @assignments = []
  @computers = Computer.all
  Computer.all.each do |asset|
    @assignments <<  Assignment.where(:computer_id => asset.id).order("assign_date ASC").last
  end
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @assignments }
    format.xls { send_data @assignments.to_xls }
end

現時点では、この代替テストを実行して ID を確認しています。

describe "GET index" do
  it "assigns assignments as @assignment" do
    Assignment.any_instance.stubs(:valid?).returns(true)
    assignment = FactoryGirl.create :associated_assignment
    get :index, {}, valid_session
    assigns(:computers).should eq([assignment])
  end
end

これは、コンピューターの ID はランダムですが、割り当ての computer_id は常に 1 である、次の結果に何かを返します。

Failure/Error: assigns(:computers).should eq([assignment])

   expected: [#<Assignment id: 12, user_id: 1, computer_id: 1, assign_date: "2008-11-11", created_at: "2012-09-10 23:59:48", updated_at: "2012-09-10 23:59:48">]
        got: [#<Computer id: 14, serial_no: "abc123", asset_tag: 12345, computer_name: "comp1", make: "dell", model: "E6400", created_at: "2012-09-10 23:59:48", updated_at: "2012-09-10 23:59:48">]
4

2 に答える 2

2

ファクトリは、何かが持つ ID を保証しません。ただし、次の方法で適切なコンピューター オブジェクトを見つけることができます。

allocation = FactoryGirl.create(:allocation)
computer = allocation.computer
于 2012-09-10T04:47:21.503 に答える
1

問題は :computer_id と :computer にあると思います。FactoryGirl のファクトリとアソシエーションを推論する機能を使用してそれを行う 1 つの方法を次に示します。

factory :computer do
  serial_no "12345"
end

factory :allocation do
  computer
end

さらに、スペックで各コンピューターに一意のシリアル番号を持たせたい場合は、次を使用します。

sequence :serial_no do |n|
  "1234#{n}"
end

factory :computer do
  serial_no
end

factory :allocation do
  computer
end

Factory Girl はモデルとその関連付けを認識しているため、それらをピックアップして関連オブジェクトの作成方法を推測できます。

したがって:

allocation = FactoryGirl.create :allocation

Allocation オブジェクトと、シリアル番号 12340 の関連付けられた Computer を作成します。Computer オブジェクトの ID は既に Allocation の computer_id フィールドにあるため、関係は完全に設定されています。allocation.computer は機能し、allocation.computer_id は allocation.computer.id と同じになります。

これは、FactoryGirl のシンタックス シュガーを使用しています。関連付け (フィールド) 名とファクトリ名を指定することで、より明確にすることができます。

factory :computer do
  serial_no "12345"
end

factory :allocation do
  association :computer, factory: :computer
end
于 2012-09-12T01:27:02.797 に答える