我运行完整套件时MinitTest Testcase失败,单独运行时成功

时间:2015-07-29 10:41:35

标签: ruby-on-rails testing capybara minitest

我对我的测试和一个测试用具特别感到困惑:

当我一起运行所有集成测试时 这个特定的测试用例给了我这个错误:


    UsersSignupCapybaraTest
    test_signup_process_with_capybara                              ERROR (5.16s)
    Capybara::ElementNotFound: Unable to find link or button "Sign up now!"

当只运行这一个测试时,它会通过:


    UsersSignupCapybaraTest
    test_signup_process_with_capybara                               PASS (10.19s)

有人可以向我解释一下吗? 我昨天问了一个类似的问题here。 我想我不了解我测试的一些基本机制。我错了,假设每个测试用例都经过隔离测试?或者一开始,之前停止了吗?这不会有意义,因为我必须照顾他们执行的命令,这听起来不对我。

这是包含测试用例的文件:


    require 'test_helper'
    class UsersSignupCapybaraTest  :chrome)
        end
        Capybara.current_driver = :selenium_chrome
      end
      test "signup process with capybara" do
        visit root_path
        click_on "Sign up now!"
        fill_in "user_name",    with: "Neuer User"
        fill_in "user_email",   with: "neuer@user.de"
        # more code ...
      end
    end
    
    

这是我的 test_helper.rb



    # set to test environment
    ENV['RAILS_ENV'] ||= 'test'
    # load up the rails application
    require File.expand_path('../../config/environment', __FILE__)
    # start minitest
    require 'rails/test_help'
    require 'minitest/rails'
    require 'minitest/rails/capybara'
    require 'capybara/rails'
    require 'capybara/poltergeist'
    require 'minitest/reporters'

    Minitest::Reporters.use!(
      Minitest::Reporters::SpecReporter.new,
      ENV
    )

    class ActiveSupport::TestCase
      ActiveRecord::Migration.check_pending!
      # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
      fixtures :all

      # Returns true if a test user is logged in.
      def is_logged_in?
        !session[:user_id].nil?
      end

      # Logs in a test user.
      def log_in_as(user, options = {})
        password    = options[:password]    || 'password'
        remember_me = options[:remember_me] || '1'
        if integration_test?
          post login_path, session: { email:       user.email,
                                      password:    password,
                                      remember_me: remember_me }
        else
          session[:user_id] = user.id
        end
      end

      private

      # Returns true inside an integration test.
      def integration_test?
        defined?(post_via_redirect)
      end
    end

    class ActionDispatch::IntegrationTest
      # Make the Capybara DSL available in all integration tests
      include Capybara::DSL

      # Logs in a test user.
      def log_in_as(user, options = {})
        password    = options[:password]    || 'password'
        remember_me = options[:remember_me] || '1'
        if integration_test?
          post login_path, session: { email:       user.email,
                                      password:    password,
                                      remember_me: remember_me }
        else
          session[:user_id] = user.id
        end
      end
    end

    # https://github.com/jnicklas/capybara#transactions-and-database-setup
    class ActiveRecord::Base  
      mattr_accessor :shared_connection
      @@shared_connection = nil

      def self.connection
        @@shared_connection || retrieve_connection
      end
    end  
    ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection 


    # register Capybara driver: Selenium
    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 

2 个答案:

答案 0 :(得分:0)

通常,当某些测试在数据库或其他数据层中创建某种状态时会发生这种情况。在你的情况下,我怀疑它与用户会话有关?也许某些用户的其他测试签名没有正确签名。如果您使用devise / warden,this可能对您有用。

首先要做的好事是尝试在save_and_open_page之后尝试将visit root_path添加到测试(docs)中,并在测试套件运行时查找页面状态并检查是否"立即注册!" -Button就在那里。

答案 1 :(得分:0)

你需要一个

def teardown
  Capybara.reset_sessions!
  Capybara.use_default_driver
end

在测试类中或其父类之一 - 最好定义一个单独的类

class CapybaraTest < ActionDispatch::IntegrationTest
  include Capybara::DSL

  def teardown
    Capybara.reset_sessions!
    Capybara.use_default_driver
    super  # this might not be necessary - or should maybe be before the other two methods - not sure off the top of my head
  end
end

然后派生出使用CapybaraTest的Capybara的测试