我安装了水豚和硒。我对登录过程进行了三个测试:


使用Minitest进行测试
使用水豚
结合Minitest和Capybara语法。


测试3在Capybara断言“ Not at Dashboard”中引发失败。

是否可以在同一测试中结合使用Minitest和Capybara的语法?如果是,我的测试3出了什么问题?



1)最小:

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

    def setup
        @user_one = users(:dagobert)
    end

    test "login with minitest" do
        # Go to login
        get login_path
        assert_template 'sessions/new'

        # Login
        log_in_as(@user_one)
        assert is_logged_in?

        # Assert redirect to dashboard
        assert_redirected_to dashboard_url
        assert_template 'dashboard/index'
    end
end




2)水豚:

require "capybara_test_helper"
require 'test_helper'

class UserLoginCapybaraTest < ActionDispatch::IntegrationTest

    def setup
        @user_one = users(:dagobert)
    end

test "login with capybara" do

    Capybara.current_driver = :selenium

    # Go to login
    visit "/login"
    assert_equal "/login", current_path
    assert page.has_content?("Log in"), "Not Log in"

    # Login
    fill_in('session_email', :with => @user_one.email)
    fill_in('session_password', :with => 'password')
    click_button "Log in"

    # Assert redirect to dashboard
    assert page.has_content?("Dashboard")
  end
end




3)Minitest和Capybara相结合,导致失败:

require "capybara_test_helper"
require 'test_helper'

class UserLoginCapybaraTest < ActionDispatch::IntegrationTest

    def setup
        @user_one = users(:dagobert)
    end

    test "login with minitest & capybara" do
        # Go to login
        get login_path
        assert_template 'sessions/new'

        # Login
        log_in_as(@user_one)
        assert is_logged_in?

        # Assert redirect to dashboard
        assert_redirected_to dashboard_url
        assert_template 'dashboard/index'

        # Check content with Capybara
        assert page.has_content?("Dashboard"), "Not at Dashboard"
    end
end

最佳答案

您不能在一次测试中结合使用#g​​et和#page来期望事情正常进行,因为它们都有自己的请求和页面内容。
  Capybara(在使用支持JS的驱动程序时)尝试通过运行浏览器并控制浏览器向应用程序发出请求来模拟用户。然后,水豚在浏览器中查询文档。您(在大多数Capybara驱动程序中)无法访问响应代码,呈现的模板等内容,因为用户通常不会看到它们,并且Capybara的目的(还是为了模仿用户)。

#get,通过直接调用应用来获得快捷方式。您可以使用Capybara.string通过Capybara提供的匹配器/断言在#get获得的响应内容上使用。

“快速跟踪”登录很大程度上取决于您用于身份验证的内容,应该将其作为一个单独的问题进行询问,其中包含有关您的应用的更多详细信息

关于ruby-on-rails - 结合Capybara和Minitest语法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34838064/

10-09 00:34