Skip to content

Commit

Permalink
Merge pull request #309 from zacviandier/rspec_on_event_arguments
Browse files Browse the repository at this point in the history
Rspec custom matcher: `on_event` with arguments
  • Loading branch information
alto committed Feb 5, 2016
2 parents b868289 + b185d61 commit 6ec081a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -778,6 +778,8 @@ expect(job).to allow_event :run
expect(job).to_not allow_event :clean
expect(job).to allow_transition_to(:running)
expect(job).to_not allow_transition_to(:cleaning)
# on_event also accept arguments
expect(job).to transition_from(:sleeping).to(:running).on_event(:run, :defragmentation)

# classes with multiple state machine
multiple = SimpleMultipleExample.new
Expand Down
8 changes: 4 additions & 4 deletions lib/aasm/rspec/transition_from.rb
Expand Up @@ -2,8 +2,7 @@
match do |obj|
@state_machine_name ||= :default
obj.aasm(@state_machine_name).current_state = from_state.to_sym
# expect(obj).to receive(:broadcast_event).with(@event.to_s, obj, from_state, @to_state)
obj.send(@event) && obj.aasm(@state_machine_name).current_state == @to_state.to_sym
obj.send(@event, *@args) && obj.aasm(@state_machine_name).current_state == @to_state.to_sym
end

chain :on do |state_machine_name|
Expand All @@ -14,12 +13,13 @@
@to_state = state
end

chain :on_event do |event|
chain :on_event do |event, *args|
@event = event
@args = args
end

description do
"transition state to :#{@to_state} from :#{expected} on event :#{@event} (on :#{@state_machine_name})"
"transition state to :#{@to_state} from :#{expected} on event :#{@event}, with params: #{@args} (on :#{@state_machine_name})"
end

failure_message do |obj|
Expand Down

0 comments on commit 6ec081a

Please sign in to comment.