mardi 6 décembre 2016

I need help to write the rspec test in rails

I have a items_controller.rb

  def get_serialized_copy_of_item
    @item= Item.find_by_id(params[:id]) 
    if @item.nil?
      head :no_content
    else
      respond_to do |format|
      serialized_item = @item.as_json(include: [:test1, :test2, :test3, :test4])
      format.html
      format.json { render json: serialized_item }  
      end
    end
  end

routes.rb

namespace :items do
  get '/get_serialized_copy_of_item/:id', to:'items#get_serialized_copy_of_item'
end

I want to write a rspec test

  1. submit incorrect item id and make sure that 204 is returned

I have done

require 'spec_helper'

describe Items::ItemsController do
  describe "GET items#get_serialized_copy_of_item" do
     it "renders 204 status code" do
      get "/items/get_serialized_copy_of_item/dfsdf"
      expect(last_response.status).to eq(204)
    end
  end
end

Error: I am getting routing error

F

Failures:

  1) Items::ItemsController GET items#item renders 204 status code
     Failure/Error: get "/items/get_serialized_copy_of_item/dfsdf"
     ActionController::RoutingError:
       No route matches {:controller=>"items/items", :action=>"/items/get_serialized_copy_of_item/dfsdf"}
     # ./spec/controllers/items/items_controller_spec.rb:6:in `block (3 levels) in <top (required)>'

Finished in 0.01576 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/items/items_controller_spec.rb:5 # Items::itemsController GET items#item renders 204 status code

Thanks

Aucun commentaire:

Enregistrer un commentaire