简体   繁体   中英

Rspec+Capybara - it doesn't make any record changes?

I wonder, when I try to test Capybara+Rspec feature like updating records, I can't catch if record was updated?

For example, I have test:

scenario "updates list" do
    @student = FactoryGirl.create(:student)
    @stream.students << @student
    FactoryGirl.create(:account, :account_holder=>@student)
    visit program_stream_path(@program, @stream)
    click_link "Редактировать список студентов"
    expect(current_path).to eq edit_students_list_stream_path(@stream)
    within("#student_0") do
      fill_in "Имя", :with => "Имя"
      fill_in "Фамилия", :with => "Фамилия"
      fill_in "Электронная почта", :with => "randommail@mail.ru"
    print page.html
    end
    #print page.html
    click_button "Сохранить изменения"
    expect(current_path).to eq program_stream_path(@program, @stream)
    expect(page).to have_content "randommail@mail.ru"
    end

With this test I can assure that there is no routing errors and action is processed, but I can't check that record actually was updated?

 expect(page).to have_content "randommail@mail.ru"

This line throws a failure, but as you can see I filled email field with this value. In browser I can see that records are updated. So, Capybara doesn't suit for testing this part of application?

EDIT: I added print page.html after commit, related output:

<table class="table spacer">
<tr>
<th>Фамилия и имя</th>
<th>Электронная почта</th>
<th>Телефон</th>
</tr>
<tr><td>Surname1 Name1</td>
<td>somemail1@mail.ru</td> #email unchanged
<td>89112223321</td>
</tr>
</table>

Logs of browser action:

{"student":[{"id":"378","account":{"name":"testname","surname":"testsurname","email":"randommail@mail.ru","phone":""}}]}


UPDATE "accounts" SET "password_digest" = $1, "email" = $2, "updated_at" = $3 WHERE "accounts"."id" = $4    1 ms
/Users/slava/RubymineProjects/lms/lib/list.rb:29        COMMIT

And email updated.

Thanks to commenters here, who gave me a clue.

I first thought that may be Capybara was not designed to test ActiveRecord interactions (as there is model and controller RSpec tests), so I didn't tried to debug scenario itself very much.

Now I pass the test.

    @student = FactoryGirl.create(:student)
    FactoryGirl.create(:account, :account_holder=>@student)

This lines were the source of issue, creating Account if speaking exact.

My FactoryGirl of student declaration was later on (before paying attention to this spec) extended with

after(:build) {|student| student.account = build(:account, :account_holder =>student )}

So here I got 2 account factories on one @student (as studying page.html I found two field groups where I should have one), and therefore changed first group, which is last created account.

But in my model Student:

has_one :account, :as => :account_holder

So after commit page output data for first Account record, which was second on form (and I updated first one, which is never shown)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM