为什么FactoryGirl创建的对象在测试中可见,但在正在测试的视图中却看不到



我使用的是RSpec、FactoryGirl和PhantomJS。


更新:

我已经验证,如果我在下面调用的spec/support/login_macros.rblogin_user方法中创建此项,则列表对象在视图中可用。

这似乎是FactoryGirl的问题。为什么我可以在那个辅助方法中从工厂创建,但不能在测试本身内部?以下是支持方法:

module LoginMacros
def login_user(admin = false)
myrepo = FactoryGirl.create(:repository, :myname)
plan = FactoryGirl.create(:plan, name: "Test Multi", listing_limit: 5000, repositories_allowed: 5)
user = FactoryGirl.create(:user)
subscription = FactoryGirl.create(:subscription, user: user, plan: plan)
user.add_repository(myrepo)
listing = FactoryGirl.create(:listing, user: user, repository: myrepo)
visit login_path
fill_in 'Email', :with => user.email
fill_in 'Password', :with => user.password
click_button 'Go'
user
end

我已在spec_helper.rb中将transactional_fixtures设置为false。这是我的规格:

require "spec_helper"
describe "App Integration" do
let!(:user) { login_user }
let!(:myrepo) { user.repositories.where(name: "myrepo" ).first }
it "lets a user add apps to a listing", js: true do
listing = FactoryGirl.create(:listing, user: user, repository: myrepo)
puts listing.inspect
Capybara::Screenshot.screenshot_and_open_image
end
end

现在问题来了。看到上面的puts行了吗?它打印出物体。

但在屏幕截图中,就好像对象从未创建过。就像魔术!

但是,用户和存储库对象在视图中都可见

此外,我可以转到另一个视图,看到Listing对象。只是不在我申请的主页上!

为什么该对象在一个视图中可见,而在另一个视图不可见?我只是在主页上做这件事:

<h3><%= Listing.count %></h3>

它总是,总是,总是为零。毫无意义。

在测试中似乎从来没有调用过visit,所以页面总是空白的。在保存屏幕截图之前,您需要调用visit root_path # (or whatever path that heading is on)

let!块在it块之前调用。由于您在let!块中查看页面,但在it块中创建了listing,因此在查看页面之后才会创建列表。

最新更新