Rails:如何测试state_machine?

发布于 2024-09-05 18:24:59 字数 689 浏览 7 评论 0原文

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

我的 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

我使用 state_machine 宝石。

请给我看看课程。

Please, help me. I'm confused. I know how to write state-driven behavior of model, but I don't know what should I write in specs...

My model.rb file look

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

I use state_machine gem.

Please, show me the course.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

(り薆情海 2024-09-12 18:25:00

这个问题很老了,但我也有同样的问题。以 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

我的解决方案是:

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

我正在使用:

  • ruby​​ (1.9.3)
  • Rails (3.1 .3)
  • rspec(2.8.0.rc1)
  • factory_girl(2.3.2)
  • state_machine(1.1.0)

The question is old, but I had the same one. Taking example from 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

My solution was:

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

I was using:

  • ruby (1.9.3)
  • rails (3.1.3)
  • rspec (2.8.0.rc1)
  • factory_girl (2.3.2)
  • state_machine (1.1.0)
醉生梦死 2024-09-12 18:25:00

state_machine_rspec gem 包含许多用于编写简洁规范的辅助方法。

 describe Ratification do
   it { should have_states :boss, :declarant, :done, :owner }
   it { should handle_events :approve, when: :boss }
   it { should handle_events :approve, when: :owner }
   it { should handle_events :divert, when: :boss }
   it { should handle_events :divert, when: :owner }
   it { should handle_events :repeat, when: :declarant }
   it { should reject_events :approve, :divert, :repeat, when: :done }
   it { should reject_events :approve, :divert, :repeat, when: :done }
 end

这些 RSpec 匹配器将从高级别协助 state_machine 规范。从这里开始,我们需要为 can_approve?can_divert?can_repeat? 的业务案例编写规范。

The state_machine_rspec gem includes many helper methods for writing concise specs.

 describe Ratification do
   it { should have_states :boss, :declarant, :done, :owner }
   it { should handle_events :approve, when: :boss }
   it { should handle_events :approve, when: :owner }
   it { should handle_events :divert, when: :boss }
   it { should handle_events :divert, when: :owner }
   it { should handle_events :repeat, when: :declarant }
   it { should reject_events :approve, :divert, :repeat, when: :done }
   it { should reject_events :approve, :divert, :repeat, when: :done }
 end

These RSpec matchers will assist with the state_machine specs from a high-level. From here, one needs to write the specs for the business cases for can_approve?, can_divert?, and can_repeat?.

尾戒 2024-09-12 18:25:00

我写了一个 RSpec 自定义匹配器。它允许以优雅而简单的方式测试状态流:检查一下

I have written a RSpec custom matcher. It allows to test state flow in elegant and simple way: check it out

岁月染过的梦 2024-09-12 18:25:00

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

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

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

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

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

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

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

Unfortunately, I think you need to put a test for each state -> state transition, which might feel like code duplication.

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

Fortunately, I think this usually fits into integration testing. For instance, an extremely simple state machine for a payments system would be:

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

You might still have the unit tests as above, but you'll probably also have integration testing, something like:

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

That was a lot of handwaiving, but hopefully that makes sense. I'm also not super used to RSpec, so hopefully I didn't make too many mistakes there. If there's a more elegant way to test this, I haven't found it yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文