【发布时间】:2018-08-23 00:22:08
【问题描述】:
我想测试我的项目的创建方法,但是这个创建方法在我的表单中有 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
但是没有用,我该怎么做?
【问题讨论】:
标签: ruby-on-rails ruby testing rspec rspec-rails