I'm trying to make a test to submit JSON data to my API endpoint, however it's not working. After trying various suggestions from blogs and the rspec documentation I'm still failing.
specs/controller/v1/devices_controller_spec.rb
require 'rails_helper' RSpec.describe V1::DevicesController, type: :controller do it "updates device info" do @data = {} @device = FactoryGirl.create(:device) @data[:diagnostic_dns] = false @data[:diagnostic_ping] = false put :update, @data.to_json @data.reload response.should be_successful end end
I've also tried this in my test:
it "updates device info" do device = FactoryGirl.create(:device) device.diagnostic_dns = false device.diagnostic_ping = false put :update, :device, :format => :json response.should be_successful end
Which results in this rspec failure:
Failures: 1) V1::DevicesController updates device info Failure/Error: put :update, @data.to_json ArgumentError: wrong number of arguments (given 2, expected 1)
app/controllers/v1/devices_controller.rb
class V1::DevicesController < ApplicationController skip_before_action :authenticate_user!, only: [:show, :update], :if => Proc.new { |c| c.request.format == 'application/json' } before_action :set_device, only: [:show, :update] respond_to :json def update if @device.update(device_params) render json: @device, status: :ok else render json: @device.errors, status: :unprocessable_entity end end private def set_device @device = Device.find_by!(serial_number: params[:serial_number]) end def device_params params.require(:device).permit( :serial_number, :name, :diagnostic_checkin_status, :diagnostic_dns, :diagnostic_ping, ) end end
And my routes.rb file
v1_device GET /v1/devices/:serial_number(.:format) v1/devices#show PATCH /v1/devices/:serial_number(.:format) v1/devices#update PUT /v1/devices/:serial_number(.:format) v1/devices#update