我想测试我的项目的create方法,但是这个create方法在我的表单中有3个步骤,我想测试所有这些。为了测试每个步骤,我需要发送一个创建请求及其各自的步骤参数。
问题是:我在每一步都重复了很多参数,我想知道如何将常用参数放在一个方法中,然后调用它。
这是我的rspec文件
require 'rails_helper'
RSpec.describe Api::MenteeApplicationsController, type: :controller do
describe "Api Mentee Application controller tests" do
let(:edition) { create(:edition) }
it 'should start create a Mentee Application, step 1' do
edition
post :create, application: {
first_name: "Mentee", last_name: "Rspec", email: "mentee@email.com",
gender: "female", country: "IN", program_country: "IN",
time_zone: "5 - Mumbai", communicating_in_english: "true",
send_to_mentor_confirmed: "true",
time_availability: 3,
previous_programming_experience: "false" },
step: "1", steps: "3"
expect(response).to have_http_status(200)
end
it 'should continue to create a Mentee Application, step 2' do
post :create, application: {
first_name: "Mentee", last_name: "Rspec", email: "mentee@email.com",
gender: "female", country: "IN", program_country: "IN",
time_zone: "5 - Mumbai", communicating_in_english: "true",
send_to_mentor_confirmed: "true",
time_availability: 3,
motivation: "Motivation",
background: "Background",
team_work_experience: "Team Work Experience",
previous_programming_experience: "false" },
step: "2", steps: "3"
expect(response).to have_http_status(200)
end
it 'should not create a Mentee Application in api format' do
applications = MenteeApplication.count
post :create, application: {
first_name: "Mentee", last_name: "Rspec", email: "mentee@email.com",
gender: "female", country: "IN", program_country: "IN",
time_zone: "5 - Mumbai", communicating_in_english: "true",
send_to_mentor_confirmed: "true",
motivation: "Motivation",
background: "Background",
team_work_experience: "Team Work Experience",
previous_programming_experience: "false", experience: "",
operating_system: "mac_os",
project_proposal: "Project Proposal",
roadmap: "Roadmap",
time_availability: 3,
engagements: ["master_student", "part_time", "volunteer", "one_project"] },
step: "3", steps: "3"
expect(response).to have_http_status(:unprocessable_entity)
expect(MenteeApplication.count).to be(0)
end
it 'should create a Mentee Application in api format (step 3)' do
applications = MenteeApplication.count
post :create, application: {
first_name: "Mentee", last_name: "Rspec", email: "mentee@email.com",
gender: "female", country: "IN", program_country: "IN",
time_zone: "5 - Mumbai", communicating_in_english: "true",
send_to_mentor_confirmed: "true",
motivation: "Motivation",
background: "Background",
programming_language: "ruby",
team_work_experience: "Team Work Experience",
previous_programming_experience: "false", experience: "",
operating_system: "mac_os",
project_proposal: "Project Proposal",
roadmap: "Roadmap",
time_availability: 3,
engagements: ["master_student", "part_time", "volunteer", "one_project"] },
step: "3", steps: "3"
expect(response).to have_http_status(200)
expect(MenteeApplication.count).to be(applications+1)
expect(flash[:notice]).to eq("Thank you for your application!")
end
end
end
如您所见,步骤1中的参数在步骤2和3中使用,所以我在想这样的事情:
def some_params
params.require(:application).permit(first_name: "Mentee", last_name: "Rspec", email: "mentee@email.com",
gender: "female", country: "IN", program_country: "IN",
time_zone: "5 - Mumbai", communicating_in_english: "true",
send_to_mentor_confirmed: "true",
time_availability: 3,
previous_programming_experience: "false")
end
但是没有用,我怎么能这样做?
答案 0 :(得分:2)
let
块允许您在测试用例(it
s)中定义要使用的变量。需要注意的一些要点:
let!
- 这会强制评估)context
s 前往RSpec docs了解更多相关信息。
您提供的代码可以使用let
s,就像这样:
require 'rails_helper'
RSpec.describe Api::MenteeApplicationsController, type: :controller do
describe "Api Mentee Application controller tests" do
let(:edition) { create(:edition) }
let(:first_step_params) do
{
first_name: 'Mentee',
last_name: 'Rspec',
#...
previous_programming_experience: false,
}
end
let(:second_step_params) do
{
motivation: "Motivation",
background: "Background",
team_work_experience: "Team Work Experience",
}.merge(first_step_params)
end
let(:third_step_params) do
{
operating_system: "mac_os",
project_proposal: "Project Proposal",
roadmap: "Roadmap",
time_availability: 3,
engagements: ["master_student", "part_time", "volunteer", "one_project"],
}.merge(third_step_params)
end
it 'should start create a Mentee Application, step 1' do
edition
post :create, application: first_step_params, step: "1", steps: "3"
expect(response).to have_http_status(200)
end
it 'should continue to create a Mentee Application, step 2' do
post :create, application: second_step_params, step: "2", steps: "3"
expect(response).to have_http_status(200)
end
it 'should not create a Mentee Application in api format' do
applications = MenteeApplication.count
post :create, application: third_step_params, step: "3", steps: "3"
expect(response).to have_http_status(:unprocessable_entity)
expect(MenteeApplication.count).to be(0)
end
end
end
控制器应该是用户界面和后台服务之间的薄软件层。他们的测试很难被认为是集成(端到端)或单元测试。
我建议你改用功能规格。 (capybara非常适合使用RSpec进行Rails测试)
此blog post可能会为此提供更多见解。
请参阅betterspecs.org。
let(:application_params) do
{
first_name: 'Mentee',
last_name: 'Rspec',
#...
previous_programming_experience: false,
}
end
它会阻止incidental changes。
内容如
--require rails_helper
因此,您不需要在每个规范文件的顶部require 'rails_helper'
。
context
s 这也是betterspecs.org的指导。你可以做点什么
RSpec.describe Api::MenteeApplicationsController, type: :controller do
describe "Api Mentee Application controller tests" do
let(:edition) { create(:edition) }
let(:application_params) do
{
#...
}
end
let(:step) { 1 }
it 'should start create a Mentee Application' do
edition
post :create, application: application_params, step: step, steps: "3"
expect(response).to have_http_status(200)
end
context 'in second step' do
let(:step) { 2 }
it 'should continue to create a Mentee Application' do
post :create, application: application_params, step: step, steps: "3"
expect(response).to have_http_status(200)
end
end
end
end
context
对于处理其他参数也可能很方便:
RSpec.describe Api::MenteeApplicationsController, type: :controller do
describe "Api Mentee Application controller tests" do
let(:edition) { create(:edition) }
let(:application_params) do
common_params.merge(additional_params)
end
let(:commom_params) do
{
#...
}
end
let(:additional_params) { {} }
it 'creates an application' do
post :create, application: application_params
end
context 'with API params' do
let(:additional_params) do
{
#...
}
end
it 'creates an application' do
post :create, application: application_params
end
end
end
end
请注意,post
方法调用在两种情况下都变得完全相同。这将允许重用它(在before
块或甚至另一个let
块中。
答案 1 :(得分:0)
我想我会想要做类似下面的事情。基本上:
创建一个名为@full_application
的memoized变量,并将其包装在一个方法中(我已在测试的底部完成此操作)。
创建常量,规定每个测试所需的值的子集,例如STEP_ONE_PARAMS
,STEP_TWO_PARAMS
等。
在每个it
块中,使用.slice
和上面定义的常量来"抓住"您想要使用的full_application
值。
这样的事情:
require 'rails_helper'
RSpec.describe Api::MenteeApplicationsController, type: :controller do
STEP_ONE_PARAMS = %w(
first_name
last_name
email
gender
country
communicating_in_english
send_to_mentor_confirmed
time_availability
previous_programming_experience
).freeze
STEP_TWO_PARAMS = STEP_ONE_PARAMS.dup.concat(%w(
motivation
background
team_work_experience
)).freeze
STEP_THREE_PARAMS = STEP_TWO_PARAMS.dup.concat(%w(
operating_system
project_proposal
roadmap
engagements
)).freeze
describe "Api Mentee Application controller tests" do
let(:edition) { create(:edition) }
it 'should start create a Mentee Application, step 1' do
edition
post :create, application: full_application.slice(*STEP_ONE_PARAMS),
step: "1", steps: "3"
expect(response).to have_http_status(200)
end
it 'should continue to create a Mentee Application, step 2' do
post :create, application: full_application.slice(*STEP_TWO_PARAMS),
step: "2", steps: "3"
expect(response).to have_http_status(200)
end
it 'should not create a Mentee Application in api format' do
applications = MenteeApplication.count
post :create, application: full_application.slice(*STEP_THREE_PARAMS),
step: "3", steps: "3"
expect(response).to have_http_status(:unprocessable_entity)
expect(MenteeApplication.count).to be(0)
end
it 'should create a Mentee Application in api format (step 3)' do
applications = MenteeApplication.count
post :create, application: full_application,
step: "3", steps: "3"
expect(response).to have_http_status(200)
expect(MenteeApplication.count).to be(applications+1)
expect(flash[:notice]).to eq("Thank you for your application!")
end
end
end
def full_application
@full_application ||= {
first_name: "Mentee",
last_name: "Rspec",
email: "mentee@email.com",
gender: "female",
country: "IN",
program_country: "IN",
time_zone: "5 - Mumbai",
communicating_in_english: "true",
send_to_mentor_confirmed: "true",
motivation: "Motivation",
background: "Background",
programming_language: "ruby",
team_work_experience: "Team Work Experience",
previous_programming_experience: "false",
experience: "",
operating_system: "mac_os",
project_proposal: "Project Proposal",
roadmap: "Roadmap",
time_availability: 3,
engagements: [
"master_student",
"part_time",
"volunteer",
"one_project"
]
}
end