简体   繁体   English

如何为state_machine过渡回调编写rspec测试

[英]How write rspec test for state_machine transition callback

I have spree Adjustment extension 我有大礼包调整扩展

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

And I have rspec test, which fails. 我有rspec测试,但失败了。 In real application method are called and all things work as expected. 在实际的应用程序中,方法被调用,并且所有事物都按预期工作。

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    

failed with 失败了

 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

State machine transition don't work if you set state explicitly, like you do, with Adjustment#state= method (in this case). 如果像使用Adjustment#state=方法那样显式设置状态(在这种情况下),则状态机转换将不起作用。 You should use events instead, for example: 您应该改用事件,例如:

adjustment.close

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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