2013-02-09 73 views
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 

任何人都可以幫忙嗎?

回答

0

看起來你正在嘗試創建一個已經存在的標題/醫院組合的指南。你有日誌的右塊 - 這條線:

Guideline Exists (0.3ms) SELECT 1 AS one FROM "guidelines" WHERE [...] 

是Rails的確保沒有重複的指導方針已經存在(你可能在你的模型中的「唯一」驗證)。它找到一個匹配,所以取消了節約交易:

(0.1ms) ROLLBACK TO SAVEPOINT active_record_1 

更改標題和/或醫院你試圖插入,它應該通過就好了。

編輯:

基於我們在評論中交談,我認爲這個問題是:你初始化@guideline@guideline = guidelines(:one),它加載一個名爲您在燈具定義的「一個」的方針文件。

但是,當您開始在Rails中運行測試時,它會自動將所有燈具加載到測試數據庫中。因此,當您嘗試使用@guideline的屬性制定新指南時,您的保證以獲得重複!

解決這個問題的最簡單的方法是在你的測試代碼來定義線的新屬性:

post :create, guideline: { 
    content: "This is my new content!", 
    hospital: "Some Random Hospital", 
    title: "Some Random Title" 
} 

希望幫助!

+0

感謝您的快速響應。這必須在正確的軌道上,但即使更改指南屬性也會得到相同的結果'參數:{「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

+0

@ user1109434 - Hm。你確定這些對還不存在嗎?對於發生的事情,這仍然是最好的解釋......「新的不管」仍然是非常通用的,並且很可能之前被使用過。嘗試一些奇怪的事情 - 比如說一個叫做「切達」的指南,叫做「弗雷德」。我可以保證_that's_不在你的數據庫中! – 2013-02-09 11:49:02

+0

我確定他們不存在。我可以在guidelines.yml中更改'one:'的值並獲得相同的消息。它可能與其他在guidelines_controller.rb上運行的測試有關嗎?我還有另一個測試,它也運行:創建。在guidelines_controller_test.rb的頂部,我有'setup do @guideline = guidelines(:one) end'所以也許第一個測試是創建指南(:一),這就是爲什麼當它試圖運行這個測試時,它說指南已經存在? – tessad 2013-02-09 11:58:03