2014-04-11 74 views
0

規格/功能/ album_spec.rb:Rspec的功能測試問題

feature "Album Pages" do 

    given(:album) { create(:album) } # by Factory_Girl 

    scenario "Edit album" do 
    visit edit_album_path album 

    fill_in "Name", with: "bar" 
    expect {click_button "Update Album"}.to change(Album.last, :name).to("bar") 
    end 

end 

錯誤:

1) Album Pages Edit album 
    Failure/Error: expect {click_button "Update Album"}.to change(Album.last, :name).to("bar") 
     name should have been changed to "bar", but is now "Gorgeous Granite Table" 
    # ./spec/features/albums_spec.rb:27:in `block (2 levels) in <top (required)>' 

應用程序工作正常,如果我點擊它重定向到專輯中的網站,按鈕其名稱顯示爲<h1>。我嘗試這樣做:

scenario "Edit album" do 
    visit edit_album_path album 

    fill_in "Name", with: "bar" 
    click_button "Update Album" 
    save_and_open_page 
    #expect {click_button "Update Album"}.to change(Album.last, :name).to("bar") 
    end 

比我得到了一個網頁與bar<h1>,所以我不知道這有什麼錯我的測試,我可以在test.log中看到:

SQL (0.7ms) UPDATE "albums" SET "name" = ?, "updated_at" = ? WHERE "albums"."id" = 1 [["name", "bar"], ["updated_at", Fri, 11 Apr 2014 11:30:00 UTC +00:00]] 

任何想法?

+0

我假設你有代碼在github上的某處,我可以查看。 – drKreso

+0

確實如此:[here](https://github.com/pawel7318/footoo)。 – pawel7318

回答

0

你需要你的變化驗證,也可以在塊,像這樣:

scenario "Edit album" do 
    visit edit_album_path album 
    fill_in "Name", with: "bar" 
    expect{click_button "Update Album"}.to change{Album.last.name} 
end 
+0

它工作得很好!請閱讀我自己的答案。我會很感激一些意見。 – pawel7318

0

閱讀drKreso's答案後,我看了gems/rspec-expectations-.../lib/rspec/matchers.rb。那裏有很多有用的例子change

You can either pass receiver and message, or a block, but not both.

我找到一個其他的方式,它可以怎麼做:

scenario "Edit album" do 

    @album = album 
    visit edit_album_path @album  
    fill_in "Name", with: "bar"  

    expect{ 
     click_button "Update Album" 
     @album.reload 
     }.to change(@album, :name).to("bar") 
    end 

但即使是現在我仍然有困惑爲什麼沒有在第一工作方式。因此,如果reload是一個關鍵,那麼change會在某處調用它以傳遞接收方?我不能這麼深入。