2014-04-11 68 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会在某处调用它以传递接收方?我不能这么深入。