RSpec tips #2

RSpec and state_machine gem

Wanna test some `after_transition` or `before_transition` code from your models on RSpec without super complicated mocks and stubs? Worry no more, here’s the real deal!

# User model
after_transition on: :register, :give_gift
# User spec
it ‘gives gift to user after registering’ do
user = User.new
transition = StateMachine::Transition.new(user, User.state_machine, :register, :pending, :registered)
transition.run_callbacks
expect(user.received_gift?).to be_truthy
end

Now go test some transitions!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.