无法理解水豚的行为 [英] Can't make sense of capybara behaviour

查看:59
本文介绍了无法理解水豚的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的Rails应用进行了集成测试:

I have this integration test for my Rails App:

require 'test_helper'

class StudyCapybaraTest < ActionDispatch::IntegrationTest
  def setup
    @user = users(:archer)
    @vocabs = @user.vocabs
    Capybara.register_driver :selenium_chrome do |app|
      Capybara::Selenium::Driver.new(app, :browser => :chrome)
    end
    Capybara.current_driver = :selenium_chrome
    #Capybara.default_wait_time = 5
    visit login_path
    fill_in "session_email",    with: @user.email
    fill_in "session_password", with: 'password'
    click_button "session_commit"
  end

  test "full study process" do
    assert_title "Home | Word Up"
    visit study_user_path(@user)
    ....
  end
end

奇怪的是,当我删除第一个测试完整学习过程的第一行时

Weirdly, when I remove the first line of the first test "full study process"

assert_title主页|上词

测试失败因为测试用户似乎没有登录。当我移动

the test fails because the test user doesn't seem to be logged in. The same problem occurs when I move

时,也会发生相同的问题,请访问study_user_path(@user)

进入设置功能(就像以前一样)。

into the setup function (as it was before).

但这不是更改有关定金的任何内容逻辑和逻辑对吧?
我唯一能想到的就是断言来得很早,而
应用程序没有时间执行满足断言所需的指令。

But that doesn't change anything about the sequence and logic, right? The only thing I can think of, is that the assertion comes to early, and the app doesn't have time to execute the instructions needed to meet the assertions.

这是一个计时问题吗?如果是的话,我如何防止它们将来发生?谢谢!

Is this a timing issue, and if so, how can I prevent them from happening in the future? Thx!

推荐答案

首先,您的直觉是一个计时问题。 click_button就是这样做的-它单击按钮。它不会等待表单发布,也不会等待任何ajax发生,等等。因此,在没有assert_title的情况下,您的测试是单击按钮,然后立即在浏览器中更改url。在浏览器中更改url将具有取消任何由click_button调用触发的表单提交或行为的作用。您需要在click_button之后等待由于单击按钮而导致页面上发生的更改,沿着

First, your intuition on it being a timing issue is correct. click_button does just that - it clicks the button. It doesn't wait for a form to post, it doesn't wait for any ajax to occur, etc. So without the assert_title your test is clicking the button, and immediately changing the url in the browser. The changing the url in the browser would have the effect of canceling any form submission or behavior that was triggered by the click_button call. You need to wait after the click_button for something that changes on the page as a result of clicking the button, along the lines of

assert_text('You are now logged in')

第二,在每次测试之前运行设置方法,因此您真的不想在那儿注册驱动程序,因为它只需要执行一次。

Secondly, the setup method is run before each test so you really don't want to be registering the driver in there, since it only needs to be done once.

这篇关于无法理解水豚的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆