我有一个控制器向我的控制器发送供应商列表,并且在正常视图中它工作正常。
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
如果我在浏览器中查看它,它再次起作用。但是,如果我用 Capybara 和 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
.
更新
出于测试目的,我直接使用控制器创建了供应商:
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 的记录,我想这并不能证明它正在将它们放入数据库中......