2

我最近将一个非常简单的 Rails 应用程序从 rspec 切换到 minitest。我也使用水豚和 factory_girl。

我有 3 个单独的集成测试文件,所有这些文件都涉及记录用户使用以下内容:

before(:each) do
  user = Factory(:user)
  visit login_path
  fill_in "Email", :with => user.email
  fill_in "Password", :with => user.password
  click_button "Log in"
end

在我切换到 minitest 之后,似乎每次测试后会话都停止了。例如,我会在名为“users_integration_test.rb”的测试文件中使用上面的代码测试登录,当它开始运行另一个测试文件时,比如“sessions_integration_test.rb”,用户已经登录,然后我可以使用再次登录上面的代码。

我的问题是:这是 rspec 和 minitest 之间的故意区别,我只需要在每次测试后注销用户?还是我在设置 minitest 时犯了错误?

我正在使用与 Minitest Railscast 中相同的 minitest_helper 文件。

4

2 回答 2

3

我不知道有什么区别,但下面的代码可能会起作用。

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "minitest/autorun"
require "capybara/rails"
require "active_support/testing/setup_and_teardown"

class IntegrationTest < MiniTest::Spec
  include Rails.application.routes.url_helpers
  include Capybara::DSL
  after do
    reset_session!
  end
  register_spec_type(/integration$/, self)
end

class HelperTest < MiniTest::Spec
  include ActiveSupport::Testing::SetupAndTeardown
  include ActionView::TestCase::Behavior
  register_spec_type(/Helper$/, self)
end
于 2012-10-31T09:34:34.133 回答
0

我用这个正确地拆掉了它。希望能帮助到你!前岛先生的回答也很可能奏效。我没试过。

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "minitest/autorun"
require "capybara/rails"
require "active_support/testing/setup_and_teardown"

Dir[Rails.root.join("test/support/**/*.rb")].each {|f| require f}

DatabaseCleaner.strategy = :truncation

class IntegrationTest < MiniTest::Spec
  include Rails.application.routes.url_helpers
  include Capybara::DSL
  register_spec_type(/integration$/, self)
  Capybara.javascript_driver = :selenium

  after do
    DatabaseCleaner.clean       # Truncate the database
    Capybara.reset_sessions!    # Forget the (simulated) browser state
    Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
  end
end

class HelperTest < MiniTest::Spec
  include ActiveSupport::Testing::SetupAndTeardown
  include ActionView::TestCase::Behavior
  register_spec_type(/Helper$/, self)
end
于 2012-12-19T21:32:01.260 回答