Skip to main content
added additional testing
Source Link
Godzilla74
  • 2.5k
  • 3
  • 33
  • 75

UPDATE I added byebug to the controller and this is what @device.errors has it in:

@messages={:diagnostic_ping=>["can't be blank"], :diagnostic_dns=>["can't be blank"]}, @details={:diagnostic_ping=>[{:error=>:blank}], :diagnostic_dns=>[{:error=>:blank}]}> 

So, the variables are not being set?

UPDATE I added byebug to the controller and this is what @device.errors has it in:

@messages={:diagnostic_ping=>["can't be blank"], :diagnostic_dns=>["can't be blank"]}, @details={:diagnostic_ping=>[{:error=>:blank}], :diagnostic_dns=>[{:error=>:blank}]}> 

So, the variables are not being set?

Updated code
Source Link
Godzilla74
  • 2.5k
  • 3
  • 33
  • 75

UPDATE After changing my test submit to:

patch :update, params: {serial_number: device.serial_number, device: @data }, :format => :json 

I'm now getting:

Failures: 1) V1::DevicesController updates device info Failure/Error: response.should be_successful expected `#<ActionDispatch::TestResponse:0x005592458af9d0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mu...:Headers:0x00559245826d10 @req=#<ActionController::TestRequest:0x005592458afcc8 ...>>, @variant=[]>>.successful?` to return true, got false 

UPDATE After changing my test submit to:

patch :update, params: {serial_number: device.serial_number, device: @data }, :format => :json 

I'm now getting:

Failures: 1) V1::DevicesController updates device info Failure/Error: response.should be_successful expected `#<ActionDispatch::TestResponse:0x005592458af9d0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mu...:Headers:0x00559245826d10 @req=#<ActionController::TestRequest:0x005592458afcc8 ...>>, @variant=[]>>.successful?` to return true, got false 
Source Link
Godzilla74
  • 2.5k
  • 3
  • 33
  • 75

rspec test to submit json data

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