如何为state_machine转换回调写rspec测试

时间:2014-02-13 09:24:14

标签: ruby-on-rails ruby rspec spree

我有狂欢调整扩展

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

1 个答案:

答案 0 :(得分:1)

如果使用Adjustment#state=方法(在本例中)明确设置状态,则状态机转换不起作用。您应该使用事件,例如:

adjustment.close