rails のアプリをテストするためにrspecを使っていたらタイトルのエラーが出てきました
1 2 | undefined local variable or method `json' for #<RSpec::ExampleGroups::CustomersController::GETIndex:0x007fa926cdc9a0> Did you mean? JSON |
こちらがテストコードです
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | require 'rails_helper' describe CustomersController, type: :request do describe 'GET #index' do before do @customers = create_list(:customer, 3) get '/customers', fotmat: json end it 'returns 200 status' do expect(response).to be_success expect(response.status).to eq(200) end it 'contains body' do json = JSON.parse(response.body) expect(json['data'][0]['id']).to eq(@customers[0].id) expect(json['data'][1]['id']).to eq(@customers[1].id) expect(json['data'][2]['id']).to eq(@customers[2].id) end end end |
最初は it’ contains body’ ,,, のところをコメントしたりしてたんですけど、
エラーログがいつまでたっても変わらないので json に該当するようなところを探してみたら
1 2 3 4 | before do @customers = create_list(:customer, 3) get '/customers', format: json # コロンが入っていないのと、 formatがタイポ end |
ものすごく初歩的なミスをしていました;;;
あと、今回はjsonを出力するAPIだったので、 formatごと決して無事グリーン来ましたー!
1 2 3 4 5 6 7 | CustomersController GET #index returns 200 status contains body Finished in 0.26729 seconds (files took 5.16 seconds to load) 2 examples, 0 failures |