+
Skip to content

Update RSpec helper to reset states with binding_event #832

@ccartano

Description

@ccartano

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载