2

我最近將一個非常簡單的rails應用程序從rspec切換到minitest。我也使用水豚和factory_girl。Minitest集成測試:會話不會被拆除?

我有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 Railscast中相同的minitest_helper文件。

+0

真的,你試過登出嗎?我認爲它會起作用 – beornborn 2012-03-19 09:42:09

+0

是的,我確實嘗試過一次(:每次)註銷。在這一點上它工作得很好,但我的問題是,我不知道是否我錯誤地設置了Minitest,因爲雖然拆除會話會自動進行測試框架。在我的測試中必須手動拆除所有東西將是一件痛苦的事情! – hangsu 2012-03-19 16:31:20

回答

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 
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