【问题标题】:Rspec Rails Test Error :Failure/Error: JSON.parse(response.body) && JSON::ParserError: 784: unexpected token at ' 'Rspec Rails 测试错误:失败/错误:JSON.parse(response.body) && JSON::ParserError: 784: '' 处的意外令牌
【发布时间】:2020-11-01 13:48:45
【问题描述】:

晚上好:

所以我对 Rails 测试世界是全新的。我一直在关注这个关于 API 和身份验证的教程来尝试学习测试:

Scotch.io - build-a-restful-json-api-with-rails-5

现在本教程适用于 Rails 5(我使用的是 Rails 6 - 可能与问题有关?)

所以发生的事情是当我运行 rails exec rspec 时,测试运行良好,直到我点击了我的 clients_spec.rb // 客户端控制器规范 POST 测试的这个块(如下所示):

describe 'Post /loadze_app/api/v1/clients' do
    let(:valid_attributes) { { client_name: 'Mega Client', street_address: '221 some address rd' } }
    context 'when the request is valid' do
      before { post '/loadze_app/api/v1/clients', params: valid_attributes }
      it 'creates a client' do
        expect(json['client_name']).to eq('Mega Client')
      end

      it 'returns status code 201' do
        expect(response).to have_http_status(201)
      end
    end
    context 'when the request is invalid' do
      before { post '/loadze_app/api/v1/clients', params: { street_address: 'Foobar' } }
      it 'returns status code 422' do
        expect(response).to have_http_status(422)
      end
      it 'returns a validation failure message' do
        expect(response.body).to match(/Validation failed: Client name can't be blank/)
      end
    end
  end

当我删除此块并再次运行测试时,一切正常。所以我有点不知所措,并努力在 Stack 和其他资源网站上找到任何相关的修复程序。

这是我在运行rails exec rspec 时遇到的错误:

  1) Clients API Post /loadze_app/api/v1/clients when the request is valid creates a client
     Failure/Error: JSON.parse(response.body)

     JSON::ParserError:
       784: unexpected token at ''
     # ./spec/support/request_spec_helper.rb:3:in `json'
     # ./spec/requests/clients_spec.rb:47:in `block (4 levels) in <main>'
     # ./spec/rails_helper.rb:42:in `block (3 levels) in <top (required)>'
     # ./spec/rails_helper.rb:41:in `block (2 levels) in <top (required)>'

以下是错误引用的所有文件:我已使用 ** Line # ** 表示该行

/spec/support/request_spec_helper.rb:3

module RequestSpecHelper
  def json
    JSON.parse(response.body) ** Line 3
  end
end

/spec/requests/clients_spec.rb:47

 describe 'Post /loadze_app/api/v1/clients' do
    let(:valid_attributes) { { client_name: 'Mega Client', street_address: '421 Rundleson Pl N.E' } }
    context 'when the request is valid' do
      before { post '/loadze_app/api/v1/clients', params: valid_attributes }
      it 'creates a client' do
        expect(json['client_name']).to eq('Mega Client') **Line 47**
      end

      it 'returns status code 201' do
        expect(response).to have_http_status(201)
      end
    end
    context 'when the request is invalid' do
      before { post '/loadze_app/api/v1/clients', params: { street_address: 'Foobar' } }
      it 'returns status code 422' do
        expect(response).to have_http_status(422)
      end
      it 'returns a validation failure message' do
        expect(response.body).to match(/Validation failed: Client name can't be blank/)
      end
    end
  end

/spec/rails_helper.rb:41/42

require 'database_cleaner'

require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)

abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line

begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  puts e.to_s.strip
  exit 1
end

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!
  config.include FactoryBot::Syntax::Methods
  config.include RequestSpecHelper, type: :request

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
    DatabaseCleaner.strategy = :transaction
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do **Line 41**
      example.run ** Line 42**
    end
  end

end

对于这个问题的任何帮助将不胜感激!如果需要更多信息,请告诉我!提前致谢!

【问题讨论】:

  • 我会在第 47 行之前放一个断点,看看response 是什么。我的假设是它不是有效的 json,这会导致测试失败。如果您是 ruby​​ 新手,byebugbinding.pry 是最流行的方法。

标签: ruby-on-rails rspec rspec-rails


【解决方案1】:

可能需要更多关于客户正在做什么的上下文,但请尝试:

before { post '/loadze_app/api/v1/clients', params: valid_attributes.to_json, as: :json }

【讨论】:

  • 救了我的命!!
猜你喜欢
  • 1970-01-01
  • 2018-02-11
  • 2011-07-05
  • 2014-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 2014-12-26
相关资源
最近更新 更多