我有一個控制器發送供應商列表到我的控制器,並在正常的看法,它工作正常。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的記錄,我猜並沒有證明它將它們放入數據庫中...