Rspec测试ActiveModel关注Rails。没有触发回调

时间:2017-10-11 21:34:09

标签: ruby-on-rails activemodel

我有一个看起来像这样的问题:

module Plantable
  extend ActiveSupport::Concern

  included do
    has_one :plant, as: :plantable

    after_update :grow_a_little

    def grow_a_little
       tree.plant.grow
    end
  end
end

我有一个看起来像这样的Rspec测试。它位于spec/model/concerns

 require 'rails_helper'

RSpec.describe Plantable do
  it { expect(Tree.new).to be_a(Plantable) }
  it { expect(Grass.new).to be_a(Plantable) }
  it { expect(Root.new).to be_a(Plantable) }

  describe '#grow_a_little' do
    context 'update to a recipe attribute' do
      subject { tree.update_attributes(name: 'Willow Tree') }

      let(:plant) { create(:plant) }
      let(:tree) { plant.tree }

      before do
        allow(tree).to receive(:plant) { plant }
      end

      it 'does update the product too' do
        expect(plant).to receive(:grow)

        subject
      end
    end
  end
end

但是,永远不会调用回调。知道为什么吗?

1 个答案:

答案 0 :(得分:0)

如果update_attributes失败,则不会运行After_update。

您确定更新已成功完成吗? update_attributes无提示失败。 请尝试使用described here