简体   繁体   中英

Rspec Testing ActiveModel Concerns Rails. Callbacks not fired

I have a concern that looks like this:

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

I have an Rspec test that looks like this. It's located inside 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

However, the callback is never called. Any idea why?

After_update not run if update_attributes failed.

Are you sure the update was completed successfully? update_attributes fails silently. Try using update! instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM