我有疯狂调整扩展

Spree::Adjustment.class_eval do
  state_machine do
    after_transition :to => :closed, :do => :some_method
  end
  def some_method
  end
end

我有rspec测试,但失败了。在实际的应用程序中,方法被调用,所有的事情都按预期工作。
describe Spree:Adjustment do
    let(:adjustment) { create(:adjustment, state: 'open') }
    it "call some_method after close" do
      adjustment.should_receive(:some_method)
      adjustment.state = "closed"
    end
end

失败与
 1) Spree::Adjustment call some_method after close
 Failure/Error: adjustment.should_receive(:some_method)
   (#<Spree::Adjustment:0x0000000751a340>).some_method(any args)
       expected: 1 time with any arguments
       received: 0 times with any arguments

最佳答案

如果像使用Adjustment#state=方法那样显式地设置状态(在本例中),则状态机转换不起作用您应该改用事件,例如:

adjustment.close

09-19 00:42