Skip to content

Commit

Permalink
support RSpec matcher have_state
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Oct 30, 2015
1 parent 9e9f3d0 commit c2e2775
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,8 @@

## unreleased

* add RSpec matcher `transition_from` (see [issue #178](https://github.com/aasm/aasm/issues/178) for details, thanks to [@thomasstephane](https://github.com/thomasstephane))
* add RSpec matcher `have_state` (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

Expand Down
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -723,20 +723,26 @@ Job.aasm.states_for_select

### Testing

AASM provides some matchers for [RSpec](http://rspec.info). 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`. Add `require 'aasm/rspec'` to your `spec_helper.rb` file and use them like this

```ruby
# classes with only the default state machine
job = Job.new
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)

# classes with multiple state machine
multiple = SimpleMultipleExample.new
expect(multiple).to transition_from(:standing).to(:walking).on_event(:walk).on(:move)
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 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)
```

## <a id="installation">Installation ##
Expand Down
22 changes: 22 additions & 0 deletions lib/aasm/rspec/have_state.rb
@@ -0,0 +1,22 @@
RSpec::Matchers.define :have_state do |state|
match do |obj|
@state_machine_name ||= :default
obj.aasm(@state_machine_name).current_state == state.to_sym
end

chain :on do |state_machine_name|
@state_machine_name = state_machine_name
end

description do
"have state #{expected} (on :#{@state_machine_name})"
end

failure_message do |obj|
"expected that :#{obj.aasm(@state_machine_name).current_state} would be :#{expected} (on :#{@state_machine_name})"
end

failure_message_when_negated do |obj|
"expected that :#{obj.aasm(@state_machine_name).current_state} would not be :#{expected} (on :#{@state_machine_name})"
end
end
22 changes: 22 additions & 0 deletions spec/unit/rspec_matcher_spec.rb
Expand Up @@ -18,4 +18,26 @@
expect(multiple).to_not transition_from(:sleeping).to(:sleeping).on_event(:start).on(:work)
end
end

describe "have_state" do
it "works for simple state machines" do
expect(simple).to have_state :initialised
expect(simple).to_not have_state :filled_out
simple.fill_out
expect(simple).to have_state :filled_out
end

it "works for multiple state machines" do
expect(multiple).to have_state(:standing).on(:move)
expect(multiple).to_not have_state(:walking).on(:move)
multiple.walk
expect(multiple).to have_state(:walking).on(:move)

expect(multiple).to have_state(:sleeping).on(:work)
expect(multiple).to_not have_state(:processing).on(:work)
multiple.start
expect(multiple).to have_state(:processing).on(:work)
end
end

end

0 comments on commit c2e2775

Please sign in to comment.