最小的集成测试:会话没有被拆除?

时间:2012-03-18 01:04:40

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

我最近将一个非常简单的rails应用程序从rspec切换到minitest。我也使用capybara和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文件。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我用它来正确地拆掉它。希望能帮助到你! Maeshima先生的回答也可能很有效。我没试过。

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