gpt4 book ai didi

ruby-on-rails - capybara 不等待ajax完成

转载 作者:行者123 更新时间:2023-12-04 13:50:53 24 4
gpt4 key购买 nike

我正在开发一个包含EmberJS前端的应用程序,该前端通过REST与Rails服务器通信。在我的应用程序中,通过向导创建了一个作业,因此我想测试是否创建了该作业,这是我的测试。

  feature 'Processing step three', :js => true do
background do
to_step_2
to_step_3
end

scenario 'Creating job', js: true do
within '#login' do
fill_in 'Email', with: @customer.email
fill_in 'Password', with: @customer.password

click_button 'Login'
end

expect { click_link 'Create job' }.to change(Job, :count).by(1)
end
end

因此,当用户填写完所有内容后,最后他们单击创建作业,就像我的测试所描述的那样。当我手动执行此操作时效果很好,但是当我运行我的规范时,我得到了。
1) Creating a new job Processing step three Creating job
Failure/Error: expect { click_link 'Create job' }.to change(Job, :count).by(1)
count should have been changed by 1, but was changed by 0
# ./spec/features/creating_a_new_job_spec.rb:103:in `block (3 levels) in <top (required)>'

现在,如果我查看测试日志,我可以看到,在插入我的工作之前, capybara 会运行该计数
Completed 200 OK in 36ms (Views: 6.0ms | ActiveRecord: 11.2ms)
Started POST "/api/jobs" for 127.0.0.1 at 2013-12-08 14:15:00 +0100
Processing by Api::JobsController#create as JSON
Parameters: {"job"=>{"description"=>"This is a test description of the service i would like to receive.", "zip"=>"2400", "available_from"=>"24 - 12 - 2013", "available_next"=>false, "customer_id"=>"1", "service_id"=>"1", "service_field_id"=>"1"}}
Unpermitted parameters: zip
(11.3ms) SELECT COUNT(*) FROM "jobs"
(1.2ms) BEGIN
SQL (2.5ms) INSERT INTO "jobs" ("available_from", "available_next", "created_at", "customer_id", "description", "service_field_id", "service_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["available_from", Tue, 24 Dec 2013 00:00:00 UTC +00:00], ["available_next", false], ["created_at", Sun, 08 Dec 2013 13:15:00 UTC +00:00], ["customer_id", 1], ["description", "This is a test description of the service i would like to receive."], ["service_field_id", 1], ["service_id", 0], ["updated_at", Sun, 08 Dec 2013 13:15:00 UTC +00:00]]
(0.5ms) COMMIT

我如何迫使 capybara 保持风帆一点?

最佳答案

正如您所说,主要问题是 capybara 没等。但是主要原因是测试没有使用一致的样式。

如果使用Capybara,则测试的行为就像真实用户一样,您应该期望一些UI更改而不是数据库更改,因为真实用户看不到db中会发生什么。

在此测试中,db断言立即执行,甚至比浏览器驱动程序向服务器发送数据的速度更快,因此数据为nil。

修理,

  • 首先删除within块。这似乎无关紧要。如果我没有记错的话,期望最好不要采取不同的行动。为安全起见,请这样做。
  • 添加UI期望。然后 capybara 将等到效果出现。

  • 所以,
    scenario 'Creating job', js: true do
    fill_in 'Email', with: @customer.email
    fill_in 'Password', with: @customer.password
    click_button 'Login'
    # Assume your page will show user name after signing in
    expect(page).to have_content(@customer.name)
    end

    如果您确实要测试数据库更改,则可以在UI期望之后添加该期望,但我不建议这样做。

    关于ruby-on-rails - capybara 不等待ajax完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20453877/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com