state_machines-rspec icon indicating copy to clipboard operation
state_machines-rspec copied to clipboard

Transition specs fails when using state_machines-activerecord

Open RemyMaucourt opened this issue 8 years ago • 4 comments

I started with state_machines, and wrote a few specs about my workflow with state_machines-rspec.

When I decided later to switch to state_machines-activerecord, my transition specs failed, and I can't see why. That kind of spec:

it { is_expected.to transition_from :idle, to_state: :received, on_event: :receive }

generates that kind of error:

Failure/Error:
    is_expected.to transition_from :idle, to_state: :received,
                                             on_event: :receive

Expected to be able to transition state from: idle to: received, on_event: receive

which is weird, considering I have this code in my model:

event :receive do
    transition idle: :received
end

Is there something I should know about the config of the rspec gem?

(I'm building a Rails 5.1 app)

RemyMaucourt avatar Nov 08 '17 14:11 RemyMaucourt

I'm having problems with this too, with neither transition_from or reject_states working. Both of these testing basic implementations of this gem on Rails 5.1.

KelseyDH avatar Jan 22 '18 09:01 KelseyDH

@KelseyDH I'll try to fix it. Can you show the whole code of the model and your Gemfile.

vtm9 avatar Jan 22 '18 10:01 vtm9

I came over this issue as well - so I dug into the gem for two hours and then found the error:

I did not define a valid subject. 🙄

Because of that, the specs just created a new instance of my model with just the state attribute. Since I set up AR validations in my model that instance was invalid. And every transition would fail because it required a valid instance.

So when the gem then called:

class StateMachinesIntrospector
  def valid_transition?(event, to_state)
    @subject.send(event) # => false, transition failed because invalid
    current_state_value == to_state # => false
  end
end

So it never passed valid_transition?, resulting in that pesky error message:

Expected to be able to transition state from: idle to: received, on_event: receive

I feel you could improve the error messages on this one, since in this case the error is not an invalid transition, but an invalid instance.

Hopefully this clear things up!

Thanks @vtm9 for the addition, even though I needed a while to appreciate it 😉

davegson avatar May 31 '18 14:05 davegson

Having a similar issue... no transition_from matcher seems to work, on a demonstrably valid subject. I'm on Rails 5.2

What can I do to make this error reproducable?

julianrubisch avatar Jun 28 '18 15:06 julianrubisch