Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
la prueba rspec falló para los parámetros

Soy nuevo en Rails, estoy tratando de escribir una prueba de especificaciones para crear el método del controlador y no puedo entender cómo pasar a los usuarios de parámetros para obtener el método. aquí está el método de creación para el controlador del proyecto

 def create @project = Project.new(project_params) @project.creator = current_user byebug if params[:project][:users].any? byebug params[:project][:users].reject!(&:empty?) @project.enrolled_user = User.find( params[:project][:users]) end byebug authorize @project respond_to do |format| if @project.save format.html { redirect_to @project, notice: 'Project was successfully created.'} format.json { render :show, status: :created, location: @project} else format.html { render :new} format.json { render json: @project.errors, status: :unprocessable_entity} end end end

proyecto_spec.rb

 context "post create" do it "creates a new project" do get :new byebug post :create,params: { user: user.id } byebug expect(project1.creator).to eq(user) expect(enrolled_user.user_id).to eq(user1.id) expect(response).to be_successful end end

Terminal

 1) ProjectsController post create creates a new project Failure/Error: if params[:project][:users].any? NoMethodError: undefined method `[]' for nil:NilClass # ./app/controllers/projects_controller.rb:38:in `create' # ./spec/requests/project_spec.rb:49:in `block (3 levels) in <main>'
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Realmente necesita una prueba del sistema en lugar de una prueba del controlador para lograr su requisito de probar la funcionalidad que describe aquí. Entonces, Rspec junto con Capybara le permitirá hacer cosas como esta

Así que usa Rails g rspec:system project y haz pruebas como

 it "Can create a project" do administrator_sign_in #some system test helper that creates a user and logs a user in if needed, there are plenty of examples on how to do helpers like this. visit '/admin/projects' click_link "New" #save_and_open_page #Uncomment this if you want to see what your form fields are, unstyled, can be very useful when trying to see what to fill in and what button and links to press and click fill_in "Name", with: "New project" fill_in "Other project fields", with: "Some Data" page.check("Name of a boolean field") #check a checkbox click_button("Create Project") project = Project.find_by name: "New project" expect(current_path).to eq("/admin/projects/#{project.id}") expect(page).to have_content("Project was successfully created") expect(project.name_of_boolean_field).to be true end

Más información sobre generadores aquí

Más información sobre las pruebas del sistema aquí

Más información sobre Carpincho aquí

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!