rails のアプリをテストするためにrspecを使っていたらタイトルのエラーが出てきました
undefined local variable or method `json' for #<RSpec::ExampleGroups::CustomersController::GETIndex:0x007fa926cdc9a0>
Did you mean? JSON
こちらがテストコードです
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 に該当するようなところを探してみたら
before do
@customers = create_list(:customer, 3)
get '/customers', format: json # コロンが入っていないのと、 formatがタイポ
end
ものすごく初歩的なミスをしていました;;;
あと、今回はjsonを出力するAPIだったので、 formatごと決して無事グリーン来ましたー!
CustomersController
GET #index
returns 200 status
contains body
Finished in 0.26729 seconds (files took 5.16 seconds to load)
2 examples, 0 failures