请帮帮我。我糊涂了。我知道如何编写模型的状态驱动行为,但我不知道应该在规范中写什么......
我的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 gem。
请告诉我这门课程。
答案 0 :(得分: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
我的解决方案是:
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
我正在使用:
答案 1 :(得分:2)
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?
编写业务案例的规范。
答案 2 :(得分:1)
我写了一个RSpec自定义匹配器。它允许以优雅和简单的方式测试状态流:check it out
答案 3 :(得分:0)
不幸的是,我认为您需要为每个州进行测试 - &gt;状态转换,可能感觉像代码重复。
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,所以希望我没有犯太多错误。如果有一种更优雅的方式来测试它,我还没有找到它。