问题描述
该项目使用 MongoDB,所以这个答案没有帮助: 1、2、3
我可以成功地执行每个测试文件,甚至是一小部分测试文件,但它们一起运行时会失败。 rspec spec/*
Finished in 23 minutes 45 seconds (files took 23.02 seconds to load)
2071 examples,1357 failures,28 pending
这个项目有点老了。 MongoDB 3.6,Ruby 2.5.1,以 Padrino 作为框架。此外,我们使用 DatabaseCleaner 和 Fabrication。很多错误显示Sinatra doesn’t kNow this ditty.
61) BlogController POST :create Microsite request should return code 12 for invalid token
Failure/Error: @response_body = JSON.parse(last_response.body)
JSON::ParserError:
784: unexpected token at '<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body { text-align:center;font-family:helvetica,arial;font-size:22px;
color:#888;margin:20px}
#c {margin:0 auto;width:500px;text-align:left}
</style>
</head>
<body>
<h2>Sinatra doesn’t kNow this ditty.</h2>
<img src='http://localhost.la/__sinatra__/404.png'>
<div id="c">
Try this:
<pre># in app.rb
class Microsite
post '/v1/microsite/blog' do
"Hello World"
end
end
</pre>
</div>
</body>
</html>
'
此外,我的老板使用 Apple,他也遇到了与 WiredTiger 有关的同样问题。但是,我使用的是 Ubuntu,并且 MongoDB 日志文件在执行测试时没有显示 WiredTiger 错误。 看来他是用系统配置修复的。显然,Rspec 试图打开所有文件以执行测试,并且进程耗尽了内存或类似的东西。我应该在 Ubuntu 中做一些类似的配置吗?
解决方法
当测试 A 单独通过,但不在整个测试套件中时,这通常意味着,之前的测试 B 改变了测试环境的状态(例如,不清理数据库或类似的东西),以便测试B 失败。
你需要做的是找到这个测试B,它改变了测试环境的状态,然后重构这个测试,让它回到一个干净的状态。
我通常通过单独运行测试 A 来完成此操作,然后查看文件夹结构,直到测试 A 失败,然后查看之前运行过哪些测试。
我查看了您的 spec_helper.rb
并看到您“按顺序”运行规范。一旦你解决了这个问题,我会建议你以随机顺序运行它们。这有助于您避免测试之间的依赖关系。请查看 Rspec documentation 了解详情。
将此配置添加到您的 spec_helper.rb
RSpec.configure do |config|
# other config
config.order = "random"
Kernel.srand config.seed
# more config
end
我还建议您在使用 response.status
解析 response.header['content-type']
之前检查 response.body
和 JSON.parse(last_response.body)
。