我正在做一个Rails 4.0.1
应用程序使用Capybara
and FactoryGirl
但我无法让我的测试正常工作。
我正在使用单表继承来制作Collection < ActiveRecord::Base
and a VideoCollection < Collection
模型。在我的测试中使用 Factory Girl 时,模型似乎没有按应有的方式实例化。
细节:
- 当我在浏览器中目视检查正在测试的视图时,它会正确显示该集合。
- 当我跑步时
print page.html
在对同一视图的测试中,该集合不会出现在页面代码中。
- 如果我进入测试控制台
rails c test
并执行FactoryGirl.create(:video_collection)
,然后视频集合就可以毫无问题地插入数据库。
代码:
我的型号:
# ------in app/models/collection.rb------
class Collection < ActiveRecord::Base
end
# ------in app/models/video_collection.rb------
class VideoCollection < Collection
end
# ------in db/schema.rb------
create_table "collections", force: true do |t|
t.string "type"
t.datetime "created_at"
t.datetime "updated_at"
t.string "name"
t.string "tile_image_link"
end
我的工厂:
FactoryGirl.define do
...
factory :collection do |collection|
factory :video_collection, class: VideoCollection, parent: :collection do |u|
u.sequence(:name) { |n| "Video Collection #{n}" }
tile_image_link "some-link.png"
type "VideoCollection"
end
end
...
end
My test:
# -----in spec/requests/video_collections_spec.rb-----
require 'spec_helper'
describe "VideoCollections" do
let(:video_collection) do
FactoryGirl.create(:video_collection)
end
...
### This test fails ###
expect(page).to have_link(video_collection.name,
href: "video/#{video_collection.id}")
测试输出:
Failure/Error: expect(page).to have_link(video_collection.name,
expected #has_link?("Video Collection 1", {:href=>"video/181"}) to return true, got false
我不明白为什么在测试运行时来自工厂的 video_collection 记录没有正确插入。更令我困惑的是,我可以使用相同的命令从控制台插入它们,没有任何问题。我的工厂代码是否有错误?
谢谢您的帮助!