RSpec 系统规格可以从模型中看到工厂数据,但不能从视图中看到
RSpec System spec can see factory data from model but not from view
我有一个系统规范,其中调用了 3 个工厂:
公司has_many商店has_many货架
在下面的代码中,我在规范和 ApplicationController 中都放置了一个调试器。当从规范调用时,所有工厂都按预期创建。然后当我访问 root_url 时,我找不到相同的记录(或根本找不到任何记录):
- Company.count returns 0
- Shop(find_by: id) returns nil(使用工厂创建的店铺id)
当我将相同的工厂提供给 seeds.rb 并将其拉入我的开发环境时,它会正确呈现页面和数据。
是什么原因导致控制器看不到本规范中的数据?
规格:
RSpec.describe "changing the current shop", type: :system do
let(:company) { create(:company, name: "Test co") }
let(:first_shop) { create(:shop_with_shelves, name: "seeded shop", company: company) }
let(:last_shelf) { create(:standby_shelf, name: "Cool Shelf", shop: first_shop) }
it "displays the name of the current shop" do
first_shop
last_shelf
# debugger here identifies all models created as expected
visit root_path
end
helper_method:
class ApplicationController < ActionController::Base
helper_method :current_shop
def current_shop
# debugger here is unable to find any models Company.count = 0
@current_shop ||= Company.first.current_shop
end
end
在视图中是:
<%= current_shop.name %>
Failure/Error: @current_shop ||= Company.first.current_shop
NoMethodError:
undefined method `current_shop' for nil:NilClass
如果进一步测试,我能够看到系统规格会引发此错误,但功能规格不会如下所示:
RSpec.describe "trying a system spec (this fails)", type: :system do
let!(:company) { create(:company) }
let!(:shop) { create(:shop, company: company, is_current: true) }
it "gets to the shops page" do
visit shops_path
end
end
RSpec.feature "trying a feature spec (this passes)", type: :feature do
let!(:company) { create(:company) }
let!(:shop) { create(:shop, company: company, is_current: true) }
scenario "gets to the shops page" do
visit shops_path
end
end
您的应用和测试都有自己的数据库连接。假设您使用的是 Rails (5.1+) 的现代版本,您需要启用事务测试并在应用程序和测试之间共享数据库连接。
我有一个系统规范,其中调用了 3 个工厂: 公司has_many商店has_many货架
在下面的代码中,我在规范和 ApplicationController 中都放置了一个调试器。当从规范调用时,所有工厂都按预期创建。然后当我访问 root_url 时,我找不到相同的记录(或根本找不到任何记录): - Company.count returns 0 - Shop(find_by: id) returns nil(使用工厂创建的店铺id)
当我将相同的工厂提供给 seeds.rb 并将其拉入我的开发环境时,它会正确呈现页面和数据。
是什么原因导致控制器看不到本规范中的数据?
规格:
RSpec.describe "changing the current shop", type: :system do
let(:company) { create(:company, name: "Test co") }
let(:first_shop) { create(:shop_with_shelves, name: "seeded shop", company: company) }
let(:last_shelf) { create(:standby_shelf, name: "Cool Shelf", shop: first_shop) }
it "displays the name of the current shop" do
first_shop
last_shelf
# debugger here identifies all models created as expected
visit root_path
end
helper_method:
class ApplicationController < ActionController::Base
helper_method :current_shop
def current_shop
# debugger here is unable to find any models Company.count = 0
@current_shop ||= Company.first.current_shop
end
end
在视图中是:
<%= current_shop.name %>
Failure/Error: @current_shop ||= Company.first.current_shop
NoMethodError:
undefined method `current_shop' for nil:NilClass
如果进一步测试,我能够看到系统规格会引发此错误,但功能规格不会如下所示:
RSpec.describe "trying a system spec (this fails)", type: :system do
let!(:company) { create(:company) }
let!(:shop) { create(:shop, company: company, is_current: true) }
it "gets to the shops page" do
visit shops_path
end
end
RSpec.feature "trying a feature spec (this passes)", type: :feature do
let!(:company) { create(:company) }
let!(:shop) { create(:shop, company: company, is_current: true) }
scenario "gets to the shops page" do
visit shops_path
end
end
您的应用和测试都有自己的数据库连接。假设您使用的是 Rails (5.1+) 的现代版本,您需要启用事务测试并在应用程序和测试之间共享数据库连接。