support RSpec matcher allow_transition_to

This commit is contained in:
Thorsten Böttger 2015-10-31 00:30:19 +13:00
parent 0f95393688
commit f4012f6a14
4 changed files with 45 additions and 2 deletions

View File

@ -2,7 +2,7 @@
## unreleased
* add RSpec matchers `have_state`, `allow_event` (see [issue #147](https://github.com/aasm/aasm/issues/147) for details)
* add RSpec matchers `have_state`, `allow_event` and `allow_transition_to` (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

@ -723,7 +723,7 @@ Job.aasm.states_for_select
### Testing
AASM provides some matchers for [RSpec](http://rspec.info): `transition_from`, `have_state`. Add `require 'aasm/rspec'` to your `spec_helper.rb` file and use them like this
AASM provides some matchers for [RSpec](http://rspec.info): `transition_from`, `have_state`, `allow_event` and `allow_transition_to`. Add `require 'aasm/rspec'` to your `spec_helper.rb` file and use them like this
```ruby
# classes with only the default state machine
@ -734,6 +734,8 @@ 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
expect(job).to allow_transition_to(:running)
expect(job).to_not allow_transition_to(:cleaning)
# classes with multiple state machine
multiple = SimpleMultipleExample.new
@ -743,12 +745,16 @@ 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 allow_transition_to(:walking).on(:move)
expect(multiple).to_not allow_transition_to(:running).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)
expect(multiple).to allow_transition_to(:processing).on(:move)
expect(multiple).to_not allow_transition_to(:sleeping).on(:move)
```
## <a id="installation">Installation ##

View File

@ -0,0 +1,22 @@
RSpec::Matchers.define :allow_transition_to do |state|
match do |obj|
@state_machine_name ||= :default
obj.aasm(@state_machine_name).states(:permitted => true).include?(state)
end
chain :on do |state_machine_name|
@state_machine_name = state_machine_name
end
description do
"allow transition to #{expected} (on :#{@state_machine_name})"
end
failure_message do |obj|
"expected that the state :#{expected} would be reachable (on :#{@state_machine_name})"
end
failure_message_when_negated do |obj|
"expected that the state :#{expected} would not be reachable (on :#{@state_machine_name})"
end
end

View File

@ -19,6 +19,21 @@ describe 'state machine' do
end
end
describe 'allow_transition_to' do
it "works for simple state machines" do
expect(simple).to allow_transition_to(:filled_out)
expect(simple).to_not allow_transition_to(:authorised)
end
it "works for multiple state machines" do
expect(multiple).to allow_transition_to(:walking).on(:move)
expect(multiple).to_not allow_transition_to(:standing).on(:move)
expect(multiple).to allow_transition_to(:processing).on(:work)
expect(multiple).to_not allow_transition_to(:sleeping).on(:work)
end
end
describe "have_state" do
it "works for simple state machines" do
expect(simple).to have_state :initialised