Receive "JSON::ParserError: 809: unexpected token at '<!DOCTYPE html>" when parsing response.body in specs
Receive "JSON::ParserError: 809: unexpected token at '<!DOCTYPE html>" when parsing response.body in specs
所以,我在解决这个问题上浪费了太多时间。我想在我的 rspec 请求规范中解析 json 响应,为此我创建了:
# support/helper_methods.rb
module HelperMethods
def json_response
JSON.parse(response.body)
end
end
# rails_helper.rb
RSpec.configure do |config|
...
config.include HelperMethods
end
并想在简单的请求规范中使用它:
# requests/post_spec.rb
require 'rails_helper'
RSpec.describe 'Posts', type: :request do
describe 'GET /posts' do
let(:post) { create(:post) }
before { post }
it 'returns status 200' do
get '/posts'
expect(response.status).to eq(200)
end
it 'renders posts' do
get '/posts'
expect(json_response).to include('Some title')
end
end
end
但是,第二个示例失败并显示消息:
# bundle exec rspec result
Failures:
1) Posts GET /posts renders posts
Failure/Error: JSON.parse(response.body)
JSON::ParserError:
809: unexpected token at '<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" media="all" href="/assets/application-04c3ae28f07e1bf734223bf526d0cdd296440ef53bcb3f80b9f093c6bf02f747.css" data-turbolinks-track="reload" />
<script src="/packs-test/js/application-dd998cb6b794ecb7e8af.js" data-turbolinks-track="reload"></script>
</head>
<body>
<p id="notice"></p>
<h1>Posts</h1>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/posts/56">Some title</a></td>
<td><a href="/posts/56/edit">Edit</a></td>
<td><a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/posts/56">Destroy</a></td>
</tr>
</tbody>
</table>
<br>
<a href="/posts/new">New Post</a>
</body>
</html>
'
# ./spec/support/helper_methods.rb:8:in `json_response'
# ./spec/requests/posts_spec.rb:21:in `block (3 levels) in <top (required)>'
Finished in 49.49 seconds (files took 0.46187 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/requests/posts_spec.rb:17 # Posts GET /posts renders posts
我试图查看这是否是某种文档类型问题,但是跳过它会给出同样的错误,但这次带有 html 标记。我相信这是非常微不足道的事情,但我看不出有什么不对。
非常感谢您对此提供帮助。
好吧,原来是我这边的错误。正如评论中提到的 dbugger,您无法将 html 文件解析为 json.
所以,我在解决这个问题上浪费了太多时间。我想在我的 rspec 请求规范中解析 json 响应,为此我创建了:
# support/helper_methods.rb
module HelperMethods
def json_response
JSON.parse(response.body)
end
end
# rails_helper.rb
RSpec.configure do |config|
...
config.include HelperMethods
end
并想在简单的请求规范中使用它:
# requests/post_spec.rb
require 'rails_helper'
RSpec.describe 'Posts', type: :request do
describe 'GET /posts' do
let(:post) { create(:post) }
before { post }
it 'returns status 200' do
get '/posts'
expect(response.status).to eq(200)
end
it 'renders posts' do
get '/posts'
expect(json_response).to include('Some title')
end
end
end
但是,第二个示例失败并显示消息:
# bundle exec rspec result
Failures:
1) Posts GET /posts renders posts
Failure/Error: JSON.parse(response.body)
JSON::ParserError:
809: unexpected token at '<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" media="all" href="/assets/application-04c3ae28f07e1bf734223bf526d0cdd296440ef53bcb3f80b9f093c6bf02f747.css" data-turbolinks-track="reload" />
<script src="/packs-test/js/application-dd998cb6b794ecb7e8af.js" data-turbolinks-track="reload"></script>
</head>
<body>
<p id="notice"></p>
<h1>Posts</h1>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/posts/56">Some title</a></td>
<td><a href="/posts/56/edit">Edit</a></td>
<td><a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/posts/56">Destroy</a></td>
</tr>
</tbody>
</table>
<br>
<a href="/posts/new">New Post</a>
</body>
</html>
'
# ./spec/support/helper_methods.rb:8:in `json_response'
# ./spec/requests/posts_spec.rb:21:in `block (3 levels) in <top (required)>'
Finished in 49.49 seconds (files took 0.46187 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/requests/posts_spec.rb:17 # Posts GET /posts renders posts
我试图查看这是否是某种文档类型问题,但是跳过它会给出同样的错误,但这次带有 html 标记。我相信这是非常微不足道的事情,但我看不出有什么不对。
非常感谢您对此提供帮助。
好吧,原来是我这边的错误。正如评论中提到的 dbugger,您无法将 html 文件解析为 json.