0
我的測試沒有成功創建指南,我找不到原因。測試創建狀態失敗
在guidelines_controller_test.rb測試是
test "should create guideline when logged in" do
sign_in users(:tester)
assert_difference('Guideline.count') do
post :create, guideline: { content: @guideline.content, hospital: @guideline.hospital, title: @guideline.title }
end
我創造guidelines_controller.rb行動
def create
@guideline = Guideline.new(params[:guideline])
respond_to do |format|
if @guideline.save
format.html { redirect_to @guideline, notice: 'Guideline was successfully created.' }
format.json { render json: @guideline, status: :created, location: @guideline }
else
format.html { render action: "new" }
format.json { render json: @guideline.errors, status: :unprocessable_entity }
end
end
末
當我嘗試運行測試失敗
1) Failure:
test_should_create_guideline_when_logged_in(GuidelinesControllerTest) [test/functional/guidelines_controller_test.rb:36]:
"Guideline.count" didn't change by 1.
<4> expected but was
<3>.
和test.log顯示(試圖複製相關位)
Processing by GuidelinesController#create as HTML
Parameters: {"guideline"=>{"content"=>"www.test.com", "hospital"=>"Test Hospital", "title"=>"Test title"}}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 781720531 LIMIT 1
(0.1ms) SAVEPOINT active_record_1
Guideline Exists (0.3ms) SELECT 1 AS one FROM "guidelines" WHERE (LOWER("guidelines"."title") = LOWER('Test title') AND "guidelines"."hospital" = 'Test Hospital') LIMIT 1
(0.1ms) ROLLBACK TO SAVEPOINT active_record_1
Rendered guidelines/_form.html.erb (256.5ms)
Completed 200 OK in 313ms (Views: 279.2ms | ActiveRecord: 0.8ms)
(0.2ms) SELECT COUNT(*) FROM "guidelines"
(0.1ms) rollback transaction
任何人都可以幫忙嗎?
感謝您的快速響應。這必須在正確的軌道上,但即使更改指南屬性也會得到相同的結果'參數:{「guideline」=> {「content」=>「new content」,「hospital」=>「new hospital」,「title 「=>」new title「}} 用戶負載(0.3ms)SELECT」users「。* FROM」users「WHERE」users「。」id「= 781720531 LIMIT 1 (0.1ms)SAVEPOINT active_record_1 準則存在ms)SELECT 1 AS FROM FROM「guidelines」WHERE(LOWER(「guidelines」,「title」)= LOWER('new title')AND「guidelines」。「hospital」='new hospital')LIMIT 1 (0.1ms )ROLLBACK TO SAVEPOINT active_record_1' – tessad 2013-02-09 11:39:59
@ user1109434 - Hm。你確定這些對還不存在嗎?對於發生的事情,這仍然是最好的解釋......「新的不管」仍然是非常通用的,並且很可能之前被使用過。嘗試一些奇怪的事情 - 比如說一個叫做「切達」的指南,叫做「弗雷德」。我可以保證_that's_不在你的數據庫中! – 2013-02-09 11:49:02
我確定他們不存在。我可以在guidelines.yml中更改'one:'的值並獲得相同的消息。它可能與其他在guidelines_controller.rb上運行的測試有關嗎?我還有另一個測試,它也運行:創建。在guidelines_controller_test.rb的頂部,我有'setup do @guideline = guidelines(:one) end'所以也許第一個測試是創建指南(:一),這就是爲什麼當它試圖運行這個測試時,它說指南已經存在? – tessad 2013-02-09 11:58:03