我正在尝试在索引视图中测试所有模型对象的存在,索引在浏览器使用中使用手动渲染就好了,所以问题必须与我的FactoryGirl工厂一样,或者我将参数传递到FactoryGirl.create
方法,虽然研究未能向我揭示我做错了什么,但有什么想法?感谢
quotes.rb
FactoryGirl.define do
factory :quote do
prev_cover true
sequence(:co_name) { |n| "Acme Co #{n}" }
co_number 9999
postcode "al1 1aa"
industry :financial_services
lives_overseas true
scheme_start_date "2018-01-01"
payment_frequency :monthly
commission_level 12
gla 1
factory :quote2 do
end
factory :quote3 do
end
end
end
quotes_page_spec.rb
require 'rails_helper'
feature 'index page' do
let(:user) { FactoryGirl.create(:user) }
let(:quote) { FactoryGirl.create(:quote, user: user, co_name: "Co1") }
let(:quote2) { FactoryGirl.create(:quote, user: user, co_name: "Co2") }
let(:quote3) { FactoryGirl.create(:quote, user: user, co_name: "Co3") }
before do
login_as(user, :scope => :user)
end
scenario 'when user view all their quotes' do
visit("/quotes")
expect(page).to have_content("Co1")
expect(page).to have_content("Co2")
expect(page).to have_content("Co3")
end
这是我得到的失败信息;
1) index page when user view all their quotes
Failure/Error: expect(page).to have_content("Co1")
expected to find text "Co1" in "Toggle navigation QuoteEngine My Quotes My Account Sign out"
# ./spec/features/quotes_page_spec.rb:15:in `block (2 levels) in <top (required)>'
答案 0 :(得分:1)
这里的问题是你正在使用let
。 let
被懒惰地评估,因此直到第一次在测试中引用它时才实际创建对象。由于您从未在测试中引用引用对象,因此永远不会创建它们。要始终创建对象,请使用let!
而不是let
答案 1 :(得分:0)
错误是我实际上并没有通过将它们声明为let
来创建多个引号。将我想要创建的引号移动到前一个块中,一切都很好,如下所示:
let(:user) { FactoryGirl.create(:user) }
before do
login_as(user, :scope => :user)
FactoryGirl.create(:quote, user: user, co_name: "Co1")
FactoryGirl.create(:quote, user: user, co_name: "Co2")
FactoryGirl.create(:quote, user: user, co_name: "Co3")
end