gpt4 book ai didi

ruby-on-rails - rails : How to test state_machine?

转载 作者:行者123 更新时间:2023-12-03 14:14:40 26 4
gpt4 key购买 nike

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

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

请告诉我类(class)。

最佳答案

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? .

关于ruby-on-rails - rails : How to test state_machine?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3047630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com