1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

support RSpec matcher allow_event

This commit is contained in:
Thorsten Böttger 2015-10-31 00:19:09 +13:00
parent c2e2775ada
commit 0f95393688
4 changed files with 50 additions and 1 deletions

View file

@ -2,7 +2,7 @@
## unreleased
* add RSpec matcher `have_state` (see [issue #147](https://github.com/aasm/aasm/issues/147) for details)
* add RSpec matchers `have_state`, `allow_event` (see [issue #147](https://github.com/aasm/aasm/issues/147) for details)
* add RSpec matcher `transition_from` (see [issue #178](https://github.com/aasm/aasm/issues/178) for details, thanks to [@thomasstephane](https://github.com/thomasstephane))
## 4.4.1

View file

@ -732,6 +732,8 @@ expect(job).to transition_from(:sleeping).to(:running).on_event(:run)
expect(job).not_to transition_from(:sleeping).to(:cleaning).on_event(:run)
expect(job).to have_state(:sleeping)
expect(job).not_to have_state(:running)
expect(job).to allow_event :run
expect(job).to_not allow_event :clean
# classes with multiple state machine
multiple = SimpleMultipleExample.new
@ -739,10 +741,14 @@ expect(multiple).to transition_from(:standing).to(:walking).on_event(:walk).on(:
expect(multiple).to_not transition_from(:standing).to(:running).on_event(:walk).on(:move)
expect(multiple).to have_state(:standing).on(:move)
expect(multiple).not_to have_state(:walking).on(:move)
expect(multiple).to allow_event(:walk).on(:move)
expect(multiple).to_not allow_event(:hold).on(:move)
expect(multiple).to transition_from(:sleeping).to(:processing).on_event(:start).on(:work)
expect(multiple).to_not transition_from(:sleeping).to(:sleeping).on_event(:start).on(:work)
expect(multiple).to have_state(:sleeping).on(:work)
expect(multiple).not_to have_state(:processing).on(:work)
expect(multiple).to allow_event(:start).on(:move)
expect(multiple).to_not allow_event(:stop).on(:move)
```
## <a id="installation">Installation ##

View file

@ -0,0 +1,22 @@
RSpec::Matchers.define :allow_event do |event|
match do |obj|
@state_machine_name ||= :default
obj.aasm(@state_machine_name).may_fire_event?(event)
end
chain :on do |state_machine_name|
@state_machine_name = state_machine_name
end
description do
"allow event #{expected} (on :#{@state_machine_name})"
end
failure_message do |obj|
"expected that the event :#{expected} would be allowed (on :#{@state_machine_name})"
end
failure_message_when_negated do |obj|
"expected that the event :#{expected} would not be allowed (on :#{@state_machine_name})"
end
end

View file

@ -40,4 +40,25 @@ describe 'state machine' do
end
end
describe "allow_event" do
it "works for simple state machines" do
expect(simple).to allow_event :fill_out
expect(simple).to_not allow_event :authorise
simple.fill_out
expect(simple).to allow_event :authorise
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)
multiple.walk
expect(multiple).to allow_event(:hold).on(:move)
expect(multiple).to allow_event(:start).on(:work)
expect(multiple).to_not allow_event(:stop).on(:work)
multiple.start
expect(multiple).to allow_event(:stop).on(:work)
end
end
end