规格/特征/ album_spec.rb:
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 :(得分: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
答案 1 :(得分:0)
在阅读drKreso's回答后,我读了gems/rspec-expectations-.../lib/rspec/matchers.rb
。 change
有很多有用的例子。
您既可以传递接收者和消息,也可以传递阻止,但不能同时传递两者。
我找到了另外一种方法:
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
会调用它来传递接收器吗?我无法走得这么深。