Created
December 13, 2013 12:23
-
-
Save ka8725/7943510 to your computer and use it in GitHub Desktop.
Custom matcher for Rspec to test state_machine status flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This custom matcher can be used to test state machine | |
# | |
# Examples | |
# | |
# it { should have_event(:status, :event_name, [:state1, :state2] => [:state3, :state4]) } | |
# it { should have_event(:status, :event_name, { | |
# :state1 => :state3, | |
# :state1 => :state4, | |
# :state3 => :state3, | |
# :state2 => :state4 | |
# }) | |
# } | |
RSpec::Matchers.define :have_event do |state_field, event_name, status_flows| | |
match do |model| | |
@event_name = event_name | |
all_transitions = [] | |
status_flows.each do |from, to| | |
from_arr = *from | |
to_arr = *to | |
from_arr.product(to_arr).each do |from, to| | |
all_transitions << OpenStruct({from: from, to: to}) | |
end | |
end | |
events = model.class.state_machines[state_field].events | |
event = events[event_name] | |
@unexpected_transitions = all_transitions.select do |transition| | |
!events.valid_for(model, from: transition.from, to: transition.to).include?(event) | |
end | |
@unexpected_transitions.empty? | |
end | |
failure_message_for_should do | |
unexpected_transitions_str = @unexpected_transitions.map do |t| | |
"#{@event_name}: :#{t.from} => :#{t.to}" | |
end.join(', ') | |
"there are unexpected transitions in the expectation: [#{unexpected_transitions_str}]" | |
end | |
def OpenStruct(params) | |
params.is_a?(OpenStruct) ? params : OpenStruct.new(params) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment