我想运行这个测试,但它给了我一个错误,我不明白为什么从几小时开始。
以下是完整错误:
FAIL["test_should_update_preference", Minitest::Result, 2.7792289879871532]
test_should_update_preference#Minitest::Result (2.78s)
Expected response to be a <3XX: redirect>, but was a <204: No Content>
Response body:
test/controllers/preferences_controller_test.rb:20:in `block in <class:PreferencesControllerTest>'
感谢所有关注我问题的人!
这是测试: 测试“应该更新首选项” patch preference_url(@preference),params:{preference:{accepted:@preference.accepted,institute_id:@preference.institute_id,preference_value:@preference.preference_value,user_id:@preference.user_id,wei_preference_value:@preference.wei_preference_value}} assert_redirected_to preferences_path 端
这是控制器:
class PreferencesController < ApplicationController
before_action :set_preference, only: [:show, :edit, :update, :destroy]
# GET /preferences
# GET /preferences.json
def index
@preferences = Preference.all
end
# GET /preferences/1
# GET /preferences/1.json
def show
end
# GET /preferences/new
def new
@preference = Preference.new
end
# GET /preferences/1/edit
def edit
end
# POST /preferences
# POST /preferences.json
def create
@preference = Preference.new(preference_params)
respond_to do |format|
if @preference.save
format.html { redirect_to preferences_path}
else
format.html { render :index }
format.json { render json: @preference.errors, status: :unprocessable_entity }
end
end
end
def create_all
@institutes = Institute.all
@institutes.each do |li|
@preference = Preference.create(institute_id: li.id, user_id: current_user.id, preference_value: 5)
end
lash[:success] = "Sie können die Präferenzen jetzt bearbeiten."
render :index
end
# PATCH/PUT /preferences/1
# PATCH/PUT /preferences/1.json
def update
if @preference.update_attributes(preference_params)
flash[:success] = "Die Präferenz wurde aktualisiert."
redirect_to preferences_path
end
end
# DELETE /preferences/1
# DELETE /preferences/1.json
def destroy
@preference.destroy
respond_to do |format|
format.html { redirect_to preferences_url }
end
end
当我在brwoser中执行更新任务时,这是我得到的日志文件:
Started PATCH "/preferences/43" for 127.0.0.1 at 2018-03-02 15:36:06 +0100
Processing by PreferencesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"CXcMiSLQdlUeJf75g/uClWaI89f/nxypzb60yphXh26sJx5E0bUu39SAEckE13GeozuCN9WQ3eBUTbPz1/WqoQ==", "preference"=>{"user_id"=>"2", "institute_id"=>"1", "preference_value"=>"1"}, "commit"=>"Speichern", "id"=>"43"}
[1m[36mPreference Load (0.3ms)[0m [1m[34mSELECT "preferences".* FROM "preferences" WHERE "preferences"."id" = ? LIMIT ?[0m [["id", 43], ["LIMIT", 1]]
[1m[35m (0.2ms)[0m [1m[36mbegin transaction[0m
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 1], ["LIMIT", 1]]
[1m[36mUser Load (0.1ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?[0m [["id", 2], ["LIMIT", 1]]
[1m[35mSQL (31.4ms)[0m [1m[33mUPDATE "preferences" SET "preference_value" = ?, "updated_at" = ? WHERE "preferences"."id" = ?[0m [["preference_value", 1], ["updated_at", "2018-03-02 14:36:06.852000"], ["id", 43]]
[1m[35m (23.5ms)[0m [1m[36mcommit transaction[0m
Redirected to http://localhost:3000/preferences
Completed 302 Found in 76ms (ActiveRecord: 55.6ms)
Started GET "/preferences" for 127.0.0.1 at 2018-03-02 15:36:06 +0100
Processing by PreferencesController#index as HTML
Rendering preferences/index.html.erb within layouts/application
[1m[36mUser Load (0.1ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?[0m [["id", 2], ["LIMIT", 1]]
[1m[36mPreference Exists (0.2ms)[0m [1m[34mSELECT 1 AS one FROM "preferences" WHERE "preferences"."user_id" = ? LIMIT ?[0m [["user_id", 2], ["LIMIT", 1]]
[1m[36mPreference Load (0.4ms)[0m [1m[34mSELECT "preferences".* FROM "preferences" WHERE "preferences"."user_id" = ?[0m [["user_id", 2]]
[1m[36mInstitute Load (0.3ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 1], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 2], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 3], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 4], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 5], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 6], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 7], ["LIMIT", 1]]
[1m[36mInstitute Load (0.4ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 8], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 9], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 10], ["LIMIT", 1]]
[1m[36mInstitute Load (0.5ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 11], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 12], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 13], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 14], ["LIMIT", 1]]
[1m[36mInstitute Load (0.3ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 15], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 16], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 17], ["LIMIT", 1]]
[1m[36mInstitute Load (0.2ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 18], ["LIMIT", 1]]
[1m[36mInstitute Load (0.2ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 19], ["LIMIT", 1]]
[1m[36mInstitute Load (0.4ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 20], ["LIMIT", 1]]
[1m[36mInstitute Load (0.1ms)[0m [1m[34mSELECT "institutes".* FROM "institutes" WHERE "institutes"."id" = ? LIMIT ?[0m [["id", 21], ["LIMIT", 1]]
Rendered preferences/index.html.erb within layouts/application (74.8ms)
Rendered layouts/_rails_default.html.erb (191.3ms)
Rendered layouts/_shim.html.erb (3.5ms)
Rendered layouts/_header.html.erb (19.9ms)
Rendered layouts/_footer.html.erb (1.4ms)
Completed 200 OK in 326ms (Views: 317.8ms | ActiveRecord: 4.4ms)
答案 0 :(得分:0)
如果您的update
方法调用失败,则在update_attributes
方法中,它无法呈现任何内容,这就是您收到No Content
错误的原因。
首先,您应该为该方法添加一个else语句,以便它不会返回没有内容。渲染某种错误状态就行了。然后你应该放一个调试器,看看它失败的原因。您可以在实例上使用.errors
方法来查看它无效的原因。
答案 1 :(得分:0)
感谢您的建议!我更新了控制器并将else语句放入控制器。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="料號">
<ItemTemplate>
<asp:Label ID="Part_No" runat="server" Text='<%# Eval("Part_No")%>' ></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="bhead" />
<ItemStyle CssClass="bbody" />
</asp:TemplateField>
<asp:TemplateField HeaderText="End user" >
<ItemTemplate>
<asp:Label ID="Dircu" runat="server" Text='<%# Eval("Dircu")%>' ></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="bhead" />
<ItemStyle CssClass="bbody" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Assembly" >
<ItemTemplate>
<asp:Label ID="Assfct" runat="server" Text='<%# Eval("Assfct")%>' ></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="bhead" />
<ItemStyle CssClass="bbody" />
</asp:TemplateField>
</Columns>
</asp:GridView>
现在,如果我将preferences_path放入else操作中,测试将会成功。这不是我测试的目标,所以我在else语句中放入了institute_path而不是preference_path。现在我失败了:
def update
if @preference.update_attributes(preference_params)
flash[:success] = "Die Präferenz wurde aktualisiert."
redirect_to preferences_path
else
flash[:error] = "Die Präferenz konnte nicht aktualisiert werden."
redirect_to preference_path
end
end
我将谷歌.error方法并应用它来报告错误是什么。