Hi Deep, With a request spec, you are going through Rails routing, so I don’t think there’s anything wrong with writing “get api_v1_path(repair)”. Like I said, if you’re not running one of the more recent versions of RSpec, you would need to leave the “params:” off the parameters. So if you really want to use that form, I would recommend trying "get ‘api_v1_repairs', id: id”.
Jack > On Jan 18, 2021, at 5:59 PM, Deep Dhanak <deep.dha...@gmail.com> wrote: > > Hi Jack, > > Thank you for the quick response. I am currently writing a request spec; > however, when I write “get '/api/v1/repairs', params: { id: repair.id > <http://repair.id/> }", I get text/html returned and not JSON. Not only that > no breakpoint gets triggered in my controller. > > I noticed that I can write this "get api_v1_path(repair)" instead and the > rspec will pass but as you mentioned, it seems I should use that syntax if I > write a feature spec. > > Again thanks for your help. > > Regards, > Deep > > On Mon, Jan 18, 2021 at 4:07 PM Jack Royal-Gordon <jac...@pobox.com > <mailto:jac...@pobox.com>> wrote: > The answer depends on what kind of spec (controller, feature, request) you > are writing, and what version of RSpec. > > My answer is for the newer versions of RSpec. Older versions may require you > to remove the “params:" > > For controller specs, use: “get :action, params: {id: id}” where “action” is > the controller action routed to by the URL. > > For request specs, use “get ‘/api/v1/repairs’, params: {id: id}" > > For feature specs, use “visit url” and I suppose you can use the capabilities > of Rails path helpers (including #url_for) or just add it to the URL (e.g. > “/api/v1/repairs?id=#i{id}"), but I can’t find any write-ups about testing > APIs with feature specs, so I’m not sure this really makes sense. > > >> On Jan 17, 2021, at 7:43 PM, Frozensoil <deep.dha...@gmail.com >> <mailto:deep.dha...@gmail.com>> wrote: >> >> Hi, >> >> I am just getting started with Rails & Rspec. I have something that looks >> like this currently in a request spec: >> >> describe 'GET repairs/index/1' do >> let!(:repair) { FactoryBot.create(:repair) } >> >> before do >> # TODO: check if there is a better way to call the get here >> get '/api/v1/repairs/' + repair.id.to_s >> end >> >> it 'returns status code 200' do >> expect(response).to have_http_status(200) >> end >> >> it 'returns a valid json response' do >> expect(response.content_type).to eq('application/json; charset=utf-8') >> end >> >> it 'returns the first repairs' do >> # TODO: currently json return 6 attributes, that is why we have eq(6), we >> should validate attributes of object >> expect(JSON.parse(response.body).size).to eq(6) >> end >> end >> >> It does work; however, I am trying to understand to see how I can first >> build the GET request in a better way. Also, the get request returns a JSON, >> should I parse the JSON into a repair object and expect on the model >> attributes? >> >> Thanks! >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "rspec" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to rspec+unsubscr...@googlegroups.com >> <mailto:rspec+unsubscr...@googlegroups.com>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/rspec/270ce60b-8e73-49ea-8b34-96db0ddfbb44n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/rspec/270ce60b-8e73-49ea-8b34-96db0ddfbb44n%40googlegroups.com?utm_medium=email&utm_source=footer>. > > > -- > You received this message because you are subscribed to a topic in the Google > Groups "rspec" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/rspec/Bkrojb8TGKA/unsubscribe > <https://groups.google.com/d/topic/rspec/Bkrojb8TGKA/unsubscribe>. > To unsubscribe from this group and all its topics, send an email to > rspec+unsubscr...@googlegroups.com > <mailto:rspec+unsubscr...@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rspec/5F96496F-1F0A-4F96-A8CB-9AEB3114042C%40pobox.com > > <https://groups.google.com/d/msgid/rspec/5F96496F-1F0A-4F96-A8CB-9AEB3114042C%40pobox.com?utm_medium=email&utm_source=footer>. > > -- > You received this message because you are subscribed to the Google Groups > "rspec" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rspec+unsubscr...@googlegroups.com > <mailto:rspec+unsubscr...@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rspec/CAKW5Rd%3DaQ6j_Pu3e51_4CctR3irMo60zzmSSREk15Fg5Ae%2BWwg%40mail.gmail.com > > <https://groups.google.com/d/msgid/rspec/CAKW5Rd%3DaQ6j_Pu3e51_4CctR3irMo60zzmSSREk15Fg5Ae%2BWwg%40mail.gmail.com?utm_medium=email&utm_source=footer>. -- You received this message because you are subscribed to the Google Groups "rspec" group. To unsubscribe from this group and stop receiving emails from it, send an email to rspec+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/268E49B8-636F-43D7-90F8-8063BFC49028%40pobox.com.