Rails 如何计算控制器动作的响应码?
给定以下控制器操作:
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'show' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
(我使用相同的视图来显示和编辑记录)
有了这个积极的测试:
test "should update basic user information" do
user = users(:jon)
user.first_name="Jonas"
put :update, :id => user.id, :merchant_user =>user.attributes
assert_response :found
user = Merchant::User.find(user.id)
assert user.first_name == "Jonas", "Should update basic user information"
end
一个否定的测试是这样的:
test "should not update user email for an existing email" do
user = users(:jon)
original_user_email = user.email
existing_user_email = users(:doe)
user.email=existing_user_email.email
put :update, :id => user.id, :merchant_user =>user.attributes
assert_response :success
user = Merchant::User.find(user.id)
assert user.email == original_user_email, "Should not update email for an exising one"
end
成功更新记录会导致 302 响应代码,我假设 rails 对于 GET 资源/:ID 默认为 302。更新记录失败会导致 200 OK。
如何计算这些响应代码以及如何覆盖它们?
谢谢