结合Capybara和Minitest语法

时间:2016-01-17 12:00:14

标签: ruby-on-rails ruby testing capybara minitest

我安装了Capybara和Selenium。我有三个登录过程测试:

  1. 使用Minitest进行测试
  2. 使用Capybara
  3. 结合Minitest和Capybara语法。
  4. 测试3引起Capybara断言失败,"不在仪表板"。

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

    1)Minitest:

    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)Capybara:

    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&amp; 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
    

1 个答案:

答案 0 :(得分:0)

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

另一方面,

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

&#34;快速跟踪&#34;登录高度依赖于您用于身份验证的内容,并且应该作为单独的问题提出,并提供有关您的应用的更多详细信息

相关问题