rails 控制器测试因状态泄漏而不确定地失败(我认为)
rails controller test failing non-deterministicly wrt state leak (I think)
我有一个标准的休息控制器(load_and_authorize_resource),我有以下允许的强参数:
def subscription_params
params.require(:subscription).permit(:email,:confirmed)
end
更新操作:
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
在我的测试中我有:
test "registered can edit confirmed" do
u = users(:registered)
sign_in u
@subscription = u.subscription
new_value = !@subscription.confirmed
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
不确定地失败(已确认的字段未更新)。我对 rails 的生态系统了解不够,无法知道问题出在哪里。我正在使用 devise 和 cancancan。
如果我删除 config.active_support.test_order = :random
,测试每次都会失败。如果我 运行 那些自行失败的测试,它们总是会通过。这让我相信状态在测试之间泄漏并导致问题,但我不知道是什么。
原来我需要打电话给 @subscription.reload
。
我有一个标准的休息控制器(load_and_authorize_resource),我有以下允许的强参数:
def subscription_params
params.require(:subscription).permit(:email,:confirmed)
end
更新操作:
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
在我的测试中我有:
test "registered can edit confirmed" do
u = users(:registered)
sign_in u
@subscription = u.subscription
new_value = !@subscription.confirmed
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
不确定地失败(已确认的字段未更新)。我对 rails 的生态系统了解不够,无法知道问题出在哪里。我正在使用 devise 和 cancancan。
如果我删除 config.active_support.test_order = :random
,测试每次都会失败。如果我 运行 那些自行失败的测试,它们总是会通过。这让我相信状态在测试之间泄漏并导致问题,但我不知道是什么。
原来我需要打电话给 @subscription.reload
。