为什么ruby rails to_json返回字符串而不是hash?

时间:2018-05-07 19:51:59

标签: ruby-on-rails json ruby rspec

我正在学习使用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',params:valid_credentials,headers:headers}之前

 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&#39; / auth / login&#39;,params:invalid_credentials,headers: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

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:4)

不要手动将params转换为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

另外,对于一个新项目,我会跳过控制器测试和go straight for request specs which are more future proof