Rails:如何测试state_machine?

pet*_*hka 17 rspec ruby-on-rails state-machine

请帮我.我糊涂了.我知道如何编写模型的状态驱动行为,但我不知道我应该在规范中写什么...

我的model.rb文件看

class Ratification < ActiveRecord::Base
  belongs_to :user

  attr_protected :status_events

  state_machine :status, :initial => :boss do
    state :boss
    state :owner
    state :declarant
    state :done

    event :approve do
      transition :boss => :owner, :owner => :done
    end

    event :divert do
      transition [:boss, :owner] => :declarant
    end

    event :repeat do
      transition :declarant => :boss
    end

  end
end
Run Code Online (Sandbox Code Playgroud)

我使用 state_machine gem.

拜托,告诉我课程.

san*_*xus 10

问题很古老,但我有同样的问题.以state_machine gem为例:

class Vehicle
  state_machine :state, :initial => :parked do
    event :park do
      transition [:idling, :first_gear] => :parked
    end

    event :ignite do
      transition :stalled => same, :parked => :idling
    end

    event :idle do
      transition :first_gear => :idling
    end

    event :shift_up do
      transition :idling => :first_gear, :first_gear => :second_gear, :second_gear => :third_gear
    end

    event :shift_down do
      transition :third_gear => :second_gear, :second_gear => :first_gear
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我的解决方案是:

describe Vehicle do

  before :each do
    @vehicle = Factory(:vehicle)
  end

  describe 'states' do
    describe ':parked' do
      it 'should be an initial state' do
        # Check for @vehicle.parked? to be true
        @vehicle.should be_parked
      end

      it 'should change to :idling on :ignite' do
        @vehicle.ignite!
        @vehicle.should be_idling
      end

      ['shift_up!', 'shift_down!'].each do |action|
        it "should raise an error for #{action}" do
          lambda {@job_offer.send(action)}.should raise_error
        end
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我用的是:

  • 红宝石(1.9.3)
  • 铁轨(3.1.3)
  • rspec(2.8.0.rc1)
  • factory_girl(2.3.2)
  • state_machine(1.1.0)


Jon*_*ock 0

不幸的是,我认为您需要对每个状态 -> 状态转换进行测试,这可能感觉像是代码重复。

describe Ratification do
  it "should initialize to :boss" do
    r = Ratification.new
    r.boss?.should == true
  end

  it "should move from :boss to :owner to :done as it's approved" do
    r = Ratification.new
    r.boss?.should == true
    r.approve
    r.owner?.should == true
    r.approve
    r.done?.should == true
  end

  # ...
end
Run Code Online (Sandbox Code Playgroud)

幸运的是,我认为这通常适合集成测试。例如,支付系统的一个极其简单的状态机是:

class Bill < ActiveRecord::Base
  belongs_to :account

  attr_protected :status_events

  state_machine :status, :initial => :unpaid do
    state :unpaid
    state :paid

    event :mark_as_paid do
      transition :unpaid => :paid
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

您可能仍然有上面的单元测试,但您可能还会有集成测试,例如:

describe Account do
  it "should mark the most recent bill as paid" do
    @account.recent_bill.unpaid?.should == true
    @account.process_creditcard(@credit_card)
    @account.recent_bill.paid?.should == true
  end
end
Run Code Online (Sandbox Code Playgroud)

这是很多人的放弃,但希望这是有道理的。我也不太习惯 RSpec,所以希望我没有犯太多错误。如果有更优雅的方法来测试这个,我还没有找到。

  • 您可以使用...@account.recent_bill.should be_unpaid (2认同)