对Rspec关联的困惑

时间:2018-09-24 12:00:27

标签: ruby-on-rails ruby rspec rspec-rails

我是Rspec的新手,但是在某些可能很基础的东西上遇到了麻烦。

在我的一种规格中,我具有以下(浓缩):

require 'rails_helper'

RSpec.describe Sophead, type: :model do
  let(:sophead) { FactoryGirl.create(:sophead) }
  let(:od) { FactoryGirl.create(:od) }
  let(:active_od_issue) { FactoryGirl.create(:od_issue, active:true)}
  describe "#issue_flag?"  do
    it "should return true if there is an active od issue" do
      od.update!(sophead_id: sophead.id)
      active_od_issue.update!(od_id:od.id)
      puts "sophead.id = #{sophead.id}"
      puts "od.id = #{od.id}"
      puts "od.sophead.id = #{od.sophead.id}"
      puts "Sophead.ods.count = #{sophead.ods.count}"
      puts "Sophead.ods.first.id = #{sophead.ods.first.id}"
      expect(sophead.issue_flag?).to be true
    end
  end
end

Sophead有很多ods

运行规范失败,并显示以下内容:

    Sophead
  #issue_flag?
sophead.id = 2882
od.id = 1344
od.sophead.id = 2882
Sophead.ods.count = 1
    should return true if there is an active od issue (FAILED - 1)

Failures:

  1) Sophead#issue_flag? should return true if there is an active od issue
     Failure/Error: puts "Sophead.ods.first.id = #{sophead.ods.first.id}"

     NoMethodError:
       undefined method `id' for nil:NilClass
     # ./spec/models/untitled_spec.rb:17:in `block (3 levels) in <top (required)>'

Finished in 2.34 seconds (files took 5.95 seconds to load)
1 example, 1 failure

我不知道Sophead.ods.count = 1如何,但Sophead.ods.first为零 怎么会这样?

奥德工厂:

FactoryGirl.define do
  factory :od do
    created_at "2018-09-19 11:58:58"
    updated_at "2018-09-19 11:58:58"
    status "Open"
    number_packages nil
    number_loose_lengths nil
    packed false
    picker_id nil
    packer_id nil
    picked false
    pick_list false
    label_type "Own van"
    tracking_number nil
    courier_latest_status nil
    priority 4
    packed_weight nil
    on_hold false
    photo nil
    pod nil
    picked_at nil
    packed_at nil
    despatched_at nil
    issue_flag false
    pod_byte_array nil
    photo_byte_array nil
    delivered false
    despatched false
    pick_started_at nil
    pack_started_at nil
    number_labels nil
    last_latitude 51.516185
    last_longitude 0.101993
    drop_number 0
    eta nil
    run_id 54
    direct false
    due_by "2018-09-19 13:00:00"
    delivery_failed false
    printing false
    oversize false
    trackpod false
    sales_order "312790"
    inv_name "A. Company & Sons"
    del_postcode "NW6 2HL"
    route
    run
    sophead
  end
end

Sophead的工厂:

FactoryGirl.define do
  factory :sophead do
    sysuser
    sales_order "1234567"
    carriage 10.52
    contactname "John Smith"
    date_amended Date.today
    del_add1 "Parkhall Trading Estate"
    del_add2 "40 Martel Road"
    del_city "London"
    del_country "UK"
    del_county "London"
    del_name "Jim Smith"
    del_postcode "SE21 8EN"
    delivery_notes "this is a delivery note"
    due_date Date.tomorrow
    flag_status 2
    salesman 17
    order_date Date.yesterday
    order_notes "this is an order note"
    order_type "O"
    order_value 50.92
    user_created 33
    flag_delete 0
    cost 20.50
    area 51
    # issue_flag false
    issue_desc nil
  end
end

谢谢。

1 个答案:

答案 0 :(得分:0)

非常感谢@SteveTurczyn深入浅出。如他所说,不是使用以下方法更新子对象的parent_id:

od.update!(sophead_id: sophead.id)

将子对象与父对象相关联

sophead.ods << od

以便Rails知道这种关系已经改变。