ruby on rails - rspec controller test for json api : ActionController::RoutingError -
just simple question render json call i'm trying test. i'm still learning rspec, , have tried , can't seem work. keep getting actioncontroller::routingerror, though defined route , call api works.
in controller have method:
class placescontroller < applicationcontroller def objects @objects = place.find(params[:id]).objects.active render json: @objects.map(&:api) end end
with render json: @objects.map(&:api), i'm calling api method in object model
class object def api { id: id, something: something, something_else: something_else, etc: etc, ... } end end
my routes file:
get "places/:id/objects" => "places#objects"
my rspec: spec/controllers/places_controller_spec.rb
describe "objects" "get properties" m = factorygirl.create :object_name, _id: "1", shape: "square" "/places/#{m._id}/objects", {}, { "accept" => "application/json" } expect(response.status).to eq 200 body = json.parse(response.body) expect(body["shape"]).to eq "square" end end
i keep getting error
failure/error: "/places/1/objects", {}, { "accept" => "application/json" } actioncontroller::routingerror: no route matches {:controller=>"places", :action=>"/places/1/objects"}
any appreciated, thanks.
because have spec in controllers
folder rspec assuming controller spec.
with controller specs don't specify whole path route actual controller method.
get "/places/#{m._id}/objects", {}
should be
get :objects, id: m._id
if don't want behaviour can disable setting config infer_spec_type_from_file_location
false
. or override spec type file declaring type on describe
describe "objects", type: :request do
- change :request
want spec be.
although recommend using directory structure dictate types of specs running.
Comments
Post a Comment