2013-04-24 31 views
1

我有一個控制器發送供應商列表到我的控制器,並在正常的看法,它工作正常。Rails水豚測試有空實例變量

class VendorsController < ApplicationController 

    respond_to :html, :json 

    def index 
    @vendor_categories = VendorCategory.where(:is_top_level => true) 
    @vendors = Vendor.includes(:vendor_tier, :vendor_categorizations, :vendor_categories).order('vendor_tier_id DESC, name ASC') 

    respond_with @vendors 
    end 
end 

在我看來,我有以下兩行:

= debug @vendors 
= debug current_user.user_vendor_choices 

其中,再次,如果我在瀏覽器中查看它是否工作。但是,如果我用水豚和RSpec測試它,它是空的。

require 'spec_helper' 

describe 'Vendors' do 
    before do 
    category = create(:vendor_category) 

    5.times do 
     vendor = create(:vendor) 
     vendor_categorization = create(:vendor_categorization, vendor: vendor, vendor_category: category) 
     p vendor 
     p category 
     p vendor_categorization 
    end 

    visit signup_path 

    @new_user = sign_up 
    end 

    before(:each) do 
    visit destroy_user_session_path 
    visit new_user_session_path 
    sign_in @new_user 
    visit vendors_path 
    end 

    it 'should save selected vendors', js: true do 
    p Vendor.includes(:vendor_tier, :vendor_categorizations, :vendor_categories).order('vendor_tier_id DESC, name ASC').count 
    end 

end 

Vendor.all和上述Vendor.includes...都返回值,但由於某種原因,在我的測試沒有顯示任何東西......得到一個Capybara::Element not found

UPDATE

出於測試目的,我與控制器創建賣方直接:

def index 

    @vendor_categories = VendorCategory.where(:is_top_level => true) 
    4.times do 
     Vendor.create({name: 'Test McTesterson', vendor_tier_id: 1}) 
    end 
    @vendors = Vendor.includes(:vendor_tier, :vendor_categorizations, :vendor_categories).order('vendor_tier_id DESC, name ASC') 

    respond_with @vendors 
    end 

規格通過。什麼 - ?這必須是FactoryGirl問題,或者由於某種原因,我的記錄在可以運行測試之前被刪除?創建它們之後對象的創建顯示了一個帶有ID的記錄,我猜並沒有證明它將它們放入數據庫中...

回答

1

原來我的spec_helper中定義的數據庫清潔器活動有點過於簡單蓬勃。我:

RSpec.configure do |config| 

    config.use_transactional_fixtures = false 

    config.before(:suite) do 
    DatabaseCleaner.strategy = :transaction 
    DatabaseCleaner.clean_with(:truncation) 
    end 

    config.before(:each) do 
    DatabaseCleaner.start 
    end 

    config.after(:each) do 
    DatabaseCleaner.clean 
    end 

end 

我不得不擺脫了第二塊,所以它現在讀取:

RSpec.configure do |config| 

    config.use_transactional_fixtures = false 

    config.before(:suite) do 
    DatabaseCleaner.strategy = :transaction 
    DatabaseCleaner.clean_with(:truncation) 
    end 

end 

和它的作品!不知道爲什麼......除了顯而易見之外,在每次測試之前/之後調用數據庫清理之前,還有什麼想法?

0

嗨我光標看了這個問題,不知道你甚至需要幫助了,但我認爲這是失敗的原因是一個基本的設置問題,你的答案只是補丁。

當你運行一個js: true規範(順便說一下,js: true應在describe線,而不是it線),短版,水豚的作品在不同的線程,這樣的實例變量在before塊創建的,不像使用常規Rspec測試,在規範中不可用。要使它們可用,您必須使用truncation清潔策略。

RSpec.configure do |config| 
    config.use_transactional_fixtures = false 

    config.before(:each, js: true) do 
    DatabaseCleaner.strategy = :truncation 
    end 

    config.before(:each) do 
    DatabaseCleaner.start 
    end 

    config.after(:each) do 
    DatabaseCleaner.clean 
    end 
end 

TL; DR運行JS測試時,截斷基本要求(除非很明顯,你正在運行的JS測試,不需要任何數據庫交互)。在運行所有其他測試時,使用事務(因爲它也快得多)。我想你的答案複製這個在某種程度上=)