Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transition specs fails when using state_machines-activerecord #9

Open
RemyMaucourt opened this issue Nov 8, 2017 · 4 comments
Open

Comments

@RemyMaucourt
Copy link

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)

@KelseyDH
Copy link

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.

@vtm9
Copy link
Contributor

vtm9 commented Jan 22, 2018

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

@davegson
Copy link

davegson commented May 31, 2018

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 😉

@julianrubisch
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants