Peter
Peter

Reputation: 729

controller test_should_update: param is missing or the value is empty

I try to test the update function in a controller class:

require 'test_helper'

class AppointmentsControllerTest < ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers
  include Warden::Test::Helpers

  setup do
    @heikoAppointment = appointments(:appointment_heiko)
    @heiko = users(:user_heiko)
  end

 test "should update appointment" do
    login_as(@heiko)
    @heiko.confirmed_at = Time.now
    patch appointment_url(@heikoAppointment), params: { appointment: {  } }
    assert_redirected_to appointment_url(@heikoAppointment)
  end

however I get this error:

ActionController::ParameterMissing: param is missing or the value is empty: appointment

In fixtures I saved some data for appointment:

appointment_heiko:
 user: user_heiko
 appointed: <%= Time.now + 2.weeks %>
 processed: <%= Time.now - 1.weeks %>
 shopping_list: shopping_list_lebensmittel
 shopper: user_shopper
 status: <%= Appointment.statuses[:finished] %>

Does somebody know how I can send the params with these data from fixtures easily so that I dont get this error anymore? I am a total beginner, any code could help!

Upvotes: 0

Views: 60

Answers (1)

max
max

Reputation: 101891

You're getting the error since the appointment key contains an empty hash and is not sent.

To get the attributes from a model you can use - you guessed it - .attributes.

So @heikoAppointment.attributes would give you the attributes from the model.

But when testing an update method you should just pass the attributes you want to update and assert that they have been changed.

You should also test that any attributes that should not be modifiable are not altered.

before do
  login_as(@heiko)
end

test "should update appointment" do
  @heiko.confirmed_at = Time.now
  patch appointment_url(@heikoAppointment), params: { appointment: { foo: 'bar' } }
  assert_redirected_to appointment_url(@heikoAppointment)
end

test "should update appointment foo" do
  patch appointment_url(@heikoAppointment), 
        params: { appointment: { foo: 'bar' } }
  @heikoAppointment.reload # refreshes model from DB
  assert_equals( 'bar', @heikoAppointment.foo )
end 

test "should not update appointment baz" do
  patch appointment_url(@heikoAppointment), 
          params: { appointment: { baz: 'woo' } }
  @heikoAppointment.reload # refreshes model from DB
  assert_not_equal( 'woo', @heikoAppointment.foo )
end 

Upvotes: 1

Related Questions