Rspec / Rails 5.2 如何测试分层父子关系中的子创建?
How to test child creation in hierarchical parent-child relationship with Rspec / Rails 5.2?
我的应用程序管理 BusinessFlows,它只能在父 BusinessArea 中创建。 BusinessFlows 控制器中的 'new' 方法是:
def new
@business_area = BusinessArea.find(params[:business_area_id])
@business_flow = BusinessFlow.new
end
用于测试的工厂工作正常,并创建了父 BusinessArea 和预期的 BusinessFlow:
FactoryBot.define do
factory :business_flow do
association :parent, factory: :business_area
playground_id {0}
name {"Test Business Flow"}
code {Faker::Code.unique.rut}
description {"This is a test Business Flow used for unit testing"}
created_by {"Fred"}
updated_by {"Fred"}
owner_id {0}
responsible_id {0}
deputy_id {0}
organisation_id {0}
status_id {0}
end
end
但是在测试'new'视图是否可以显示时,控制器由于缺少参数而引发错误[:business_area_id]:
ActiveRecord::RecordNotFound:
Couldn't find BusinessArea without an ID
这是功能测试脚本:
require 'rails_helper'
RSpec.describe BusinessFlow, type: :request do
include Warden::Test::Helpers
describe "Business Flows pages: " do
let(:bf) {FactoryBot.create(:business_flow)}
context "when not signed in " do
it "should propose to log in when requesting index view" do
get business_flows_path
follow_redirect!
expect(response.body).to include('Sign in')
end
it "should propose to log in when requesting new view" do
get new_business_flow_path
follow_redirect!
expect(response.body).to include('Sign in')
end
it "should propose to log in when requesting edit view" do
get edit_business_flow_path(bf)
follow_redirect!
expect(response.body).to include('Sign in')
end
it "should propose to log in when requesting show view" do
get business_flow_path(bf)
follow_redirect!
expect(response.body).to include('Sign in')
end
end
context "when signed in" do
before do
get "/users/sign_in"
test_user = FactoryBot.create(:user)
login_as test_user, scope: :user
end
it "should display index" do
get business_flows_path
expect(response).to render_template(:index)
end
it "should display new view" do
get new_business_flow_path(bf.parent.id)
expect(response).to render_template(:_form)
end
it "should display edit view" do
get edit_business_flow_path(bf)
expect(response).to render_template(:_form)
end
it "should display show view" do
get business_flow_path(bf)
expect(response).to render_template(:show)
end
end
end
end
只有上下文 'when signed in' 中的 'new' 方法测试失败。
你能帮我解决这个问题吗?
非常感谢!
it "should display new view" do
get new_business_flow_path(business_area_id: bf.parent)
expect(response).to render_template(:_form)
end
Rails 认为您传递的 id 是业务流程路径的一部分(实际上不是),我认为它需要作为参数传递。
我的应用程序管理 BusinessFlows,它只能在父 BusinessArea 中创建。 BusinessFlows 控制器中的 'new' 方法是:
def new
@business_area = BusinessArea.find(params[:business_area_id])
@business_flow = BusinessFlow.new
end
用于测试的工厂工作正常,并创建了父 BusinessArea 和预期的 BusinessFlow:
FactoryBot.define do
factory :business_flow do
association :parent, factory: :business_area
playground_id {0}
name {"Test Business Flow"}
code {Faker::Code.unique.rut}
description {"This is a test Business Flow used for unit testing"}
created_by {"Fred"}
updated_by {"Fred"}
owner_id {0}
responsible_id {0}
deputy_id {0}
organisation_id {0}
status_id {0}
end
end
但是在测试'new'视图是否可以显示时,控制器由于缺少参数而引发错误[:business_area_id]:
ActiveRecord::RecordNotFound: Couldn't find BusinessArea without an ID
这是功能测试脚本:
require 'rails_helper'
RSpec.describe BusinessFlow, type: :request do
include Warden::Test::Helpers
describe "Business Flows pages: " do
let(:bf) {FactoryBot.create(:business_flow)}
context "when not signed in " do
it "should propose to log in when requesting index view" do
get business_flows_path
follow_redirect!
expect(response.body).to include('Sign in')
end
it "should propose to log in when requesting new view" do
get new_business_flow_path
follow_redirect!
expect(response.body).to include('Sign in')
end
it "should propose to log in when requesting edit view" do
get edit_business_flow_path(bf)
follow_redirect!
expect(response.body).to include('Sign in')
end
it "should propose to log in when requesting show view" do
get business_flow_path(bf)
follow_redirect!
expect(response.body).to include('Sign in')
end
end
context "when signed in" do
before do
get "/users/sign_in"
test_user = FactoryBot.create(:user)
login_as test_user, scope: :user
end
it "should display index" do
get business_flows_path
expect(response).to render_template(:index)
end
it "should display new view" do
get new_business_flow_path(bf.parent.id)
expect(response).to render_template(:_form)
end
it "should display edit view" do
get edit_business_flow_path(bf)
expect(response).to render_template(:_form)
end
it "should display show view" do
get business_flow_path(bf)
expect(response).to render_template(:show)
end
end
end
end
只有上下文 'when signed in' 中的 'new' 方法测试失败。 你能帮我解决这个问题吗?
非常感谢!
it "should display new view" do
get new_business_flow_path(business_area_id: bf.parent)
expect(response).to render_template(:_form)
end
Rails 认为您传递的 id 是业务流程路径的一部分(实际上不是),我认为它需要作为参数传递。