Problem with rspec check function

specifications / features / 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

Error:

  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)>'

The application works fine, and if I press the button, it is redirected to the album site, where its name is displayed as <h1>. I tried this:

  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

than I got a page with barhow <h1>, so I don’t know what happened to my test, and I see in 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]]

Any ideas?

0
source share
2 answers

You need your change check to also be in a block, for example:

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
source

After reading drKreso's answer, I read gems/rspec-expectations-.../lib/rspec/matchers.rb. There are many useful examples for change.

You can either send a receiver and a 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 -? .

0

Source: https://habr.com/ru/post/1535577/


All Articles