[英]Why is ruby rails to_json returning string instead of hash?
我正在學習使用ruby版本2.3.1和rails版本5.0.7構建JSON api的教程。 該教程位於https://scotch.io/tutorials/build-a-restful-json-api-with-rails-5-part-two運行RSpec測試時,我得到:
失敗:
1)當請求有效時,AuthenticationController POST / auth / login返回認證令牌失敗/錯誤:在{post'auth / login'之前,參數:valid_credentials,標頭:標頭}
NoMethodError:
undefined method `symbolize_keys' for "{\"email\":\"foo@bar.com\",\"password\":\"foobar\"}":String
# ./spec/controllers/authentication_controller_spec.rb:32:in `block (4 levels) in <top (required)>'
# ./spec/rails_helper.rb:85:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:84:in `block (2 levels) in <top (required)>'
2)AuthenticationController POST / auth / login請求無效時返回失敗消息錯誤/錯誤:{post'/ auth / login'之前,參數:invalid_credentials,標頭:headers}
NoMethodError:
undefined method `symbolize_keys' for #<String:0x007fcb133e88f8>
# ./spec/controllers/authentication_controller_spec.rb:41:in `block (4 levels) in <top (required)>'
# ./spec/rails_helper.rb:85:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:84:in `block (2 levels) in <top (required)>'
它所引用的AuthenticationControllerSpec如下:
require 'rails_helper'
RSpec.describe AuthenticationController, type: :controller do
# Authentication test suite
describe 'POST /auth/login' do
# create test user
let!(:user) { create(:user) }
# set headers for authorization
let(:headers) { valid_headers.except('Authorization') }
# set test valid and invalid credentials
let(:valid_credentials) do
{
email: user.email,
password: user.password
}.to_json
end # let(:valid_credentials) do
let(:invalid_credentials) do
{
email: Faker::Internet.email,
password: Faker::Internet.password
}.to_json
end # let(:invalid_credentials) do
# set request.headers to our custom headers
# before { allow(request).to receive(:headers).and_return(headers) }
# returns auth token when request is valid
context 'when request is valid' do
before { post 'auth/login', params: valid_credentials, headers: headers }
it 'returns an authentication token' do
expect(json['auth_token']).not_to be_nil
end # it 'returns an authentication token' do
end # context 'when request is valid' do
# returns failure message when request is invalid
context 'When request is invalid' do
before { post '/auth/login', params: invalid_credentials, headers: headers }
it 'returns a failure message' do
expect(json['message']).to match(/Invalid credentials/)
end # it 'returns a failure message' do
end # context 'When request is invalid' do
end # describe 'POST /auth/login' do
end # RSpec.describe AuthenticationController, type: :controller do
我認為唯一其他相關文件是此模塊:
module RequestSpecHelper
# Parse JSON response to ruby hash
def json
JSON.parse(response.body)
end
end
感謝您的協助。
不要手動將參數轉換為JSON。 RSpec會自動為您執行此操作,並期望參數的哈希而不是字符串。
let(:valid_credentials) do
{
email: user.email,
password: user.password
}
end
let(:invalid_credentials) do
{
email: Faker::Internet.email,
password: Faker::Internet.password
}
end
另外,對於一個新項目,我將跳過控制器測試, 直接尋求更規范的請求規范 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.