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

Update RSpec helper to reset states with binding_event #832

Open
ccartano opened this issue Jun 7, 2023 · 0 comments
Open

Update RSpec helper to reset states with binding_event #832

ccartano opened this issue Jun 7, 2023 · 0 comments

Comments

@ccartano
Copy link

ccartano commented Jun 7, 2023

Is your feature request related to a problem? Please describe.
When using binding_event: rspec does not reset the bound state causing failures when calling transitions_from(:state).on(:second_state) in the same block

Describe the solution you'd like
I can see two routes for this, either we should be updating the rspec helper to allow the user to specify the multiple states used in the binding event, or reload the object's initial state before runing the transitions_from test

transitions_from(status: :pending, status_2: :draft).to(:cancelled, :missing) or something of the sort. Being able to set the bound event's state and the currently state in the transitions_from

Describe alternatives you've considered
Making the object reload after each test, could solve the problem, though can slow down tests significantly since we have to go pull the object back from the db before each run. Even worse would be to set each state in its own it block since we then would be recreating the subject after each test causing even more of a slow down.
Currently using an after: :method block instead of the binding_event does work, though the use of the binding event would be much cleaner to use.

Additional context
Example
In this example we can move status to cancelled only from draft.
if status_2 goes to missing, we want to tranisition status to cancelled through the binding_event, but only if the status is draft.

aasm column: "status" do
   state :draft, initial: true
   state :cancelled
   state :pending

  event :cancel do
     transitions from: [:draft], to: :cancel
  end
end

aasm :status_2, column: "status_2" do
   state :draft
   state :open, initial: true
   state :missing

  event :miss, binding_event: :cancel do
     transitions to: :missing
  end
end

RSpec test:

it do
  is_expected.to transition_from(:open).to(:missing).on_event(:miss).on(:status_2)
  # the above test will pass, but the object is now set as `status: canceled, status_2: missing`
  # when running the next test, the helper sets status_2 to `missing`, but leaves status in `cancelled` instead of reverting to 
     `draft` causing a failure in the tests below
  is_expected.to transition_from(:draft).to(:missing).on_event(:miss).on(:status_2)
  . . .
end
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

1 participant