0

Rails アプリにモデルがあります。

has_many Swots の Sales_Opportunity。

FactoryGirl を使用してそれらを設定し、テストを実行して、Sales_Opportunity を削除すると、関連する Swot も削除されることを示しています。Byebug でデバッグすると、何らかの理由で奇妙な結果が得られます。Sales_Opportunity と Swot レコードは正しく作成されていますが、sales_opportunity.swots を実行すると [ ] が返されますが、sales_opportunity.swots.count は 1 を返します。同じコードは、別のオブジェクトの関連付けで正常に機能します (timeline_events は、swot とまったく同じですが、同じコードで完全に機能します)。

誰が私が間違っているのか教えてもらえますか?

Sales_Opportunity.rb:

class SalesOpportunity < ActiveRecord::Base
 default_scope { order('close_date ASC') }
 belongs_to :user
 belongs_to :company
 has_many :key_contacts
 has_many :timeline_events, dependent: :destroy
 has_many :swots, dependent: :destroy
end

Swot.rb:

class Swot < ActiveRecord::Base
 belongs_to :sales_opportunity
 validates :swot_details, presence: true
 validates :sales_opportunity_id, presence: true
 enum swot_type: [:strength, :weakness, :opportunity, :threat]
 enum swot_importance: { minimal: 1, marginal: 2, noteworthy: 3, significant: 4, critical: 5 }
 validates :swot_importance, presence: true
end

Swot FactoryGirl 仕様:

FactoryGirl.define do
factory :swot do
    swot_importance             "minimal"
    swot_details                "Some boring details"
    swot_type                   "threat"

    trait :attached do
        association             :sales_opportunity, :with_user_id
    end
 end
end

Sales_Opportunity FactoryGirl 仕様:

FactoryGirl.define do
sequence(:opportunity_name) { |n| "Sales Oppotunity - #{n}" }

factory :sales_opportunity do
    user
    opportunity_name {generate(:opportunity_name)}
    close_date                  "2014/12/12"
    sale_value                  10000
    company_id                  7

    trait :with_user_id do
        user_id                     6
    end
end
end

Rspec テストの失敗:

describe "when swot's parent sales opportunity is destroyed" do
    let(:swot) { FactoryGirl.create(:swot, :attached) }
    let(:sales_opportunity) { swot.sales_opportunity }

it "should destroy associated swots" do
    dswots = sales_opportunity.swots.to_a
    byebug
    sales_opportunity.destroy
    expect(dswots).not_to be_empty
    dswots.each do |dswot|
    expect(Swot.where(id: dswot.id)).to be_empty
  end
end
  end

swot を記録するときのコンソール (byebug) からの出力:

#<Swot id: 13, swot_type: 3, swot_importance: 1, sales_opportunity_id: 564, swot_details: "Some boring details", created_at: "2015-07-27 10:57:23", updated_at: "2015-07-27 10:57:23">

sales_opportunity を記録するときのコンソールからの出力:

#<SalesOpportunity id: 564, close_date: "2014-12-12 00:00:00", user_id: 6, created_at: "2015-07-27 10:57:23", updated_at: "2015-07-27 10:57:23", pipeline_status: 0, opportunity_name: "Sales Oppotunity - 4", company_id: 7, sale_value: #<BigDecimal:7fe9ffd25078,'0.1E5',9(27)>, swot_score: 0>

sales_opportunity.swots.count の出力:

(byebug) sales_opportunity.swots.count
 1

sales_opportunity.swots の出力:

(byebug) sales_opportunity.swots
#<ActiveRecord::Associations::CollectionProxy []>

私はすべての既知の情報を含めたと思います。Rspec テスト、FactoryGirl ファクトリ、および sales_opportunities と Swots/Timeline_Events の間のセットアップはまったく同じですが、Rspec テストは Timeline_Events に合格し、collection_proxy はそれらに対して機能します (私の知る限り、コードは同じです)。

Timeline_Event ファクトリ:

FactoryGirl.define do
factory :timeline_event do
    activity                    "Some activity"
    due_date                    "2014/11/11"

    trait :attached do
        association             :sales_opportunity, :with_user_id
    end
end
end

Rspec テストの作業:

describe "when sales opportunity is destroyed for timeline event" do
    let(:timeline_event) { FactoryGirl.create(:timeline_event, :attached) }
    let(:sales_opportunity) { timeline_event.sales_opportunity }

it "should destroy associated timeline events" do
    timeline_events = sales_opportunity.timeline_events.to_a
    sales_opportunity.destroy
    expect(timeline_events).not_to be_empty
    timeline_events.each do |event|
    expect(TimelineEvent.where(id: event.id)).to be_empty
  end
end
end

Timeline_Event.rb:

class TimelineEvent < ActiveRecord::Base
 belongs_to :sales_opportunity
 validates :activity, presence: true 
 validates :due_date, presence: true
 validates :sales_opportunity_id, presence: true
end

ここで同じ場所で byebug を実行すると、Timeline_Event を含む配列が取得されます。

私のコードで何が問題なのかを理解してくれる人はいますか?

ありがとう。

4

2 に答える 2

1

これで問題を解決しました - アクティブなレコードの関連付けを永続化するには、sales_opportunity をリロードする必要があるようです。この答えが解決への鍵です。

作業コードは次のとおりです。

    describe "when swot's parent sales opportunity is destroyed" do
     let!(:swot) { FactoryGirl.create(:swot, sales_opportunity: sales_opportunity) }

it "should destroy associated swots" do
    sales_opportunity.reload
    expect { sales_opportunity.destroy }.to change(sales_opportunity.swots, :count).by(-1)
 end
end

上記の Max の回答のいくつかの要素を使用することで、コードのルック アンド フィールを改善することもできました。

于 2015-07-28T02:09:26.587 に答える
1
RSpec.describe SalesOpportunity, type: :model do

  let(:sales_opportunity) { create(:sales_opportunity) }

  describe "swots" do
    let!(:swot) { create(:swot, sales_opportunity: sales_opportunity) }

    it "destroys nested swots" do
      sales_opportunity.destroy
      swot.reload
      expect(swot.destroyed?).to be_truthy
    end
  end
end

これを SalesOpportunity 仕様に追加していることに注意してください。これは、依存破棄動作が子関連付けではなく SalesOpportunity に属しているためです。

編集。

この仕様を記述する別の方法は次のとおりです。

it "destroys nested swots" do
  expect {
    sales_opportunity.destroy
  }.to change(Swot, :count).by(-1)
end
于 2015-07-27T13:03:07.323 に答える