使用 RSpec 个具有 belongs_to 个关联的共享示例组
Using RSpec shared example groups with belongs_to associations
在一个 Rails 项目中(rails 5.2.2,ruby 2.4.1)我定义了 2 个资源,一个名为 "groups",一个名为 "products"。产品对象与组有 belongs_to 关系。我想创建一个可以测试这两种资源的 rspec 共享示例组,但是我在 "products".[=13 的 "create" 和 "update" 操作上遇到了一些问题=]
我想设置一组共享的示例组,它将用于创建新记录的散列作为参数。然后可以在 "groups_spec.rb" 和 "products_spec.rb" 中调用示例组。我有 "groups" 和 "products. The following is a code example for the "requests/products_spec.rb" 的固定装置,它调用共享示例:
RSpec.describe "Products", type: :request do
fixtures :groups, :products
it_should_behave_like("modify data checks",
Rails.application.routes.url_helpers.api_products_path,
Product,
{ product: {
name: "New Product",
description: "Test product to add or modify",
group_id: Group.first.id,
label: "NP"
} })
end
end
产品的问题是新产品数据需要 group_id,它必须在示例组的上下文中有效,但我必须能够从外部检索 group_id示例组以便将其传入。
我猜真正的答案是重组示例组的结构,所以我将就如何重组共享示例组提出建议。当然,如果我只是在这里做错了什么,我也会接受这个答案。
我在 it_behaves_like
块内使用 let
如下所示:
RSpec.describe "Products", type: :request do
fixtures :groups, :products
# This shared example probably lives in another file, which is fine
# I don't usually pass in args, instead using everything via `let`
shared_example 'modify_data_checks' do
# you have access to all your variables via let inside here
before do
visit(path)
end
expect(model).to be_a(Product)
expect(group_id).to eq(1)
end
it_behaves_like 'modify_data_checks' do
let(:path) { Rails.application.routes.url_helpers.api_products_path }
let(:model) { Product }
let(:group_id) { Group.first.id }
let(:params) {
product: {
name: "New Product",
description: "Test product to add or modify",
group_id: group_id,
label: "NP"
}
}
end
end
你应该能够像这样相对干净地传递数据。我们使用与此类似的模式来测试使用共享示例的多态关系。
在一个 Rails 项目中(rails 5.2.2,ruby 2.4.1)我定义了 2 个资源,一个名为 "groups",一个名为 "products"。产品对象与组有 belongs_to 关系。我想创建一个可以测试这两种资源的 rspec 共享示例组,但是我在 "products".[=13 的 "create" 和 "update" 操作上遇到了一些问题=]
我想设置一组共享的示例组,它将用于创建新记录的散列作为参数。然后可以在 "groups_spec.rb" 和 "products_spec.rb" 中调用示例组。我有 "groups" 和 "products. The following is a code example for the "requests/products_spec.rb" 的固定装置,它调用共享示例:
RSpec.describe "Products", type: :request do
fixtures :groups, :products
it_should_behave_like("modify data checks",
Rails.application.routes.url_helpers.api_products_path,
Product,
{ product: {
name: "New Product",
description: "Test product to add or modify",
group_id: Group.first.id,
label: "NP"
} })
end
end
产品的问题是新产品数据需要 group_id,它必须在示例组的上下文中有效,但我必须能够从外部检索 group_id示例组以便将其传入。
我猜真正的答案是重组示例组的结构,所以我将就如何重组共享示例组提出建议。当然,如果我只是在这里做错了什么,我也会接受这个答案。
我在 it_behaves_like
块内使用 let
如下所示:
RSpec.describe "Products", type: :request do
fixtures :groups, :products
# This shared example probably lives in another file, which is fine
# I don't usually pass in args, instead using everything via `let`
shared_example 'modify_data_checks' do
# you have access to all your variables via let inside here
before do
visit(path)
end
expect(model).to be_a(Product)
expect(group_id).to eq(1)
end
it_behaves_like 'modify_data_checks' do
let(:path) { Rails.application.routes.url_helpers.api_products_path }
let(:model) { Product }
let(:group_id) { Group.first.id }
let(:params) {
product: {
name: "New Product",
description: "Test product to add or modify",
group_id: group_id,
label: "NP"
}
}
end
end
你应该能够像这样相对干净地传递数据。我们使用与此类似的模式来测试使用共享示例的多态关系。