作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请帮我。我很困惑。我知道如何编写模型的状态驱动行为,但我不知道我应该在规范中写什么......
我的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_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
state_machine
来自高级别的规范。从这里开始,需要为
can_approve?
的业务案例编写规范。 ,
can_divert?
, 和
can_repeat?
.
关于ruby-on-rails - rails : How to test state_machine?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3047630/
我是一名优秀的程序员,十分优秀!