2015-11-05 28 views
0

如何將固定裝置的使用分離到特定的測試?如何將固定裝置分離到特定的導軌測試

在我的設置中,我的一些測試依賴於夾具數據,有些則沒有,所以在test_helper.rb中使用fixtures :all加載我所有燈具的默認設置打破了我的測試。

需要空行爲學家表

例集成測試:

require 'test_helper' 

class WelcomeFlowTest < ActionDispatch::IntegrationTest 
    test "when no user is found start welcome flow" do 
    get "/" 
    follow_redirect! 
    assert_response :success 

    post "/setup", { 
     behaviorist: { name: "Andy", email: "[email protected]" }, 
     habit: { name: "Interval running", on_monday: false, on_tuesday: true, \ 
       on_wednesday: false, on_thursday: true, on_friday: false, \ 
       on_saturday: true, on_sunday: false } 
    } 
    assert_response :success 
    assert_equal 1, Behaviorist.count 
    assert_equal 1, Habit.count 
    end 
end 

我的單元測試,要求行爲主義夾具:

require 'test_helper' 

class BehavioristTest < ActiveSupport::TestCase 
    test "validates uniqueness of :name" do 
    andy = Behaviorist.new(name: "Andy", remote_ip: "127.0.0.1") 
    assert_not run.valid? 
    assert_match /has already been taken/, andy.errors[:name].join 
    end 
end 

回答

1

關於如何Rails的一個小挖實現燈具我看到燈具,一旦加載,通過交易與每個TestCase中的更改隔離。我的工作解決方案是刪除在test_helper.rb中加載fixtures :all。然後,對於需要燈具的每個測試,我會覆蓋使用事務燈具的默認設置,加載特定的燈具,然後在拆解時將其刪除。

require 'test_helper' 

class BehavioristTest < ActiveSupport::TestCase 
    self.use_transactional_fixtures = false 
    fixtures :behaviorists 
    teardown :delete_behaviorists 

    test "validates uniqueness of :name" do 
    andy = Behaviorist.new(name: "Andy", remote_ip: "127.0.0.1") 
    assert_not run.valid? 
    assert_match /has already been taken/, run.errors[:name].join 
    end 

    private 

    def delete_behaviorists 
    Behaviorist.delete_all 
    end 
end 

分離的固定裝置單個測試用例的實施例