Question

I am creating a simple API to perform CRUD actions on contacts. :index, :show, :create and :update methods are working properly and they all pass their respective request rspec tests. The problem only arise in the :destroy method. The :destroy action looks like this:

class Api::V1::ContactsController < ApplicationController

  def destroy
    @contact = Contact.find(params[:id])
    @contact.destroy

    respond_to do |format|
      format.json {render action: :index }
    end
  end
end 

The spec to test DELETE request looks like this:

 describe "DELETE /api/v1/contacts/:id" do
  before(:each) do
   @contact = FactoryGirl.create(:contact)
  end

  it "it should have a status code of 200" do
    delete "/api/v1/contacts/#{@contact.id}"
    expect(response.status).to eq(200)
  end

  it "It should delete a contact" do
    expect{
      delete "/api/v1/contacts/#{@contact.id}"
    }.to change(Contact, :count).by(-1)
  end
end

when i run the test i get this error message:

Failure/Error: delete "/api/v1/contacts/#{@contact.id}"
 ActionController::UnknownFormat:
   ActionController::UnknownFormat
# ./app/controllers/api/v1/contacts_controller.rb:67:in `destroy'

respond_to do |format| is line 67 in contacts_controller.rb

Was it helpful?

Solution

Try changing the format of your request to JSON, i.e. replace

delete "/api/v1/contacts/#{@contact.id}"

by

delete "/api/v1/contacts/#{@contact.id}.json"

that would allow the format logic to figure out that you want the reply rendered in JSON which it knows about.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top