Search code examples
ruby-on-railsruby-on-rails-4rspecrspec3aasm

How to test assm guard error with rspec?


I have a state machine with some guards to prevent special state transitions. In my spec, I'm trying to expect guard violation error like this:

expect(violate_guard).to raise_exception

As a result, I receive correct error in my spec test:

Failure/Error: expect(my_model.change_event).to raise_exception AASM::InvalidTransition: Event 'change_event' cannot transition from 'current_state'

I'm wondering how should I change my spec to have a satisfied test?


Solution

  • you could try:

    expect { violate_guard }.to raise_error
    

    http://rubydoc.info/gems/rspec-expectations/frames