rails controller test failing non-deterministicly wrt state leak (I think)

71 Views Asked by At

I have a standard rest controller (with load_and_authorize_resource) for which I have the following strong params allowed:

def subscription_params
   params.require(:subscription).permit(:email,:confirmed)
end

update action:

  def update
    respond_to do |format|
      if @subscription.update(subscription_params)
        format.html { redirect_to @subscription, notice: 'Subscription was successfully updated.' }
        format.json { render :show, status: :ok, location: @subscription }
      else
        format.html { render :edit }
        format.json { render json: @subscription.errors, status: :unprocessable_entity }
      end
    end
  end

In my test I have:

test "registered can edit confirmed" do
  u = users(:registered)
  sign_in u
  @subscription = u.subscription
  new_value = [email protected]
  patch :update, id: @subscription, subscription: { confirmed: new_value, email: @subscription.email, token: @subscription.token }
  assert_response :redirect
  assert_equal new_value, u.subscription.confirmed
  assert_redirected_to subscription_path(@subscription)
  assert_includes flash[:notice], "Subscription was successfully updated."
end

which fails non-deterministically (the confirmed field isn't updated). I don't know enough about rails' ecosystem to know where the problem is. I'm using devise and cancancan.

If I remove config.active_support.test_order = :random the test fails every time. And If I run the tests that fail on their own they always pass. Which lead me to believe that state is leaking between tests and causing issues but I can't figure out what.

1

There are 1 best solutions below

0
On BEST ANSWER

Turns out I needed to call @subscription.reload.