From 0800c533636c9ba082a420ddc824848122059322 Mon Sep 17 00:00:00 2001 From: Cedric ZH Date: Sun, 27 Nov 2016 21:36:30 +0800 Subject: [PATCH] Allow custom params for allow_event matcher --- lib/aasm/rspec/allow_event.rb | 6 +++++- spec/models/simple_example.rb | 10 ++++++++++ spec/unit/rspec_matcher_spec.rb | 5 +++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/aasm/rspec/allow_event.rb b/lib/aasm/rspec/allow_event.rb index 18a06e8..1b31aa8 100644 --- a/lib/aasm/rspec/allow_event.rb +++ b/lib/aasm/rspec/allow_event.rb @@ -1,13 +1,17 @@ RSpec::Matchers.define :allow_event do |event| match do |obj| @state_machine_name ||= :default - obj.aasm(@state_machine_name).may_fire_event?(event) + obj.aasm(@state_machine_name).may_fire_event?(event, *@args) end chain :on do |state_machine_name| @state_machine_name = state_machine_name end + chain :with do |*args| + @args = args + end + description do "allow event #{expected} (on :#{@state_machine_name})" end diff --git a/spec/models/simple_example.rb b/spec/models/simple_example.rb index d396ab2..007d13b 100644 --- a/spec/models/simple_example.rb +++ b/spec/models/simple_example.rb @@ -11,5 +11,15 @@ class SimpleExample event :authorise do transitions :from => :filled_out, :to => :authorised end + + event :fill_out_with_args do + transitions :guard => [:arg_is_valid?], + :from => :initialised, + :to => :filled_out + end + end + + def arg_is_valid?(arg) + return arg end end diff --git a/spec/unit/rspec_matcher_spec.rb b/spec/unit/rspec_matcher_spec.rb index bdad5d0..021ff23 100644 --- a/spec/unit/rspec_matcher_spec.rb +++ b/spec/unit/rspec_matcher_spec.rb @@ -63,6 +63,11 @@ describe 'state machine' do expect(simple).to allow_event :authorise end + it "works with custom arguments" do + expect(simple).to allow_event(:fill_out_with_args).with(true) + expect(simple).to_not allow_event(:fill_out_with_args).with(false) + end + it "works for multiple state machines" do expect(multiple).to allow_event(:walk).on(:move) expect(multiple).to_not allow_event(:hold).on(:move)