Ruby on rails 即使在重新提交后,也未通过(先前通过)RSpec测试
我正在看这本书。我在第八章。在使用RSpec 3.6进行测试时,我遇到了以前没有遇到的错误。即使在我Ruby on rails 即使在重新提交后,也未通过(先前通过)RSpec测试,ruby-on-rails,ruby,api,rspec,factory-bot,Ruby On Rails,Ruby,Api,Rspec,Factory Bot,我正在看这本书。我在第八章。在使用RSpec 3.6进行测试时,我遇到了以前没有遇到的错误。即使在我git checkout提交这些测试时,我也能得到它们。我相信他们是的,因为我正在写一篇关于Rails 5.1.3中的这本书的博客文章。通过在第8章末尾运行整个测试套件,我发现它们没有通过(失败的规范来自第7章和第6章)。在我运行规范之前,主要是针对特定的文件 所以我认为这个问题可能与系统有关,但我在应用程序之外的终端上做的唯一事情是: $ swapoff -a $ swapon -a 而且可能
git checkout
提交这些测试时,我也能得到它们。我相信他们是的,因为我正在写一篇关于Rails 5.1.3中的这本书的博客文章。通过在第8章末尾运行整个测试套件,我发现它们没有通过(失败的规范来自第7章和第6章)。在我运行规范之前,主要是针对特定的文件
所以我认为这个问题可能与系统有关,但我在应用程序之外的终端上做的唯一事情是:
$ swapoff -a
$ swapon -a
而且可能sudo会得到更新
所以我现在陷入困境
错误:
出现此错误的情况是,FactoryGirl
正在创建4个产品实例,而测试在对#index
操作的GET请求后获得6个实例
产品\u控制器\u规范rb:
require 'rails_helper'
RSpec.describe Api::V1::ProductsController, type: :controller do
...
describe "GET #index" do
before(:each) do
4.times { FactoryGirl.create :product }
end
context "when is not receiving any product_ids parameter" do
before(:each) do
get :index
end
it "returns 4 records from the database" do
products_response = json_response[:products]
expect(products_response).to have(4).items
end
...
end
...
end
...
end
错误消息:
Api::V1::ProductsController GET #index when is not receiving any product_ids parameter returns 4 records from the database
Failure/Error: expect(products_response).to have(4).items
expected 4 items, got 6
如果我在测试失败之前放置了产品\u响应
,我得到了json,其中包含以下六条记录:
[
{
"id": 1,
"title": "Audible Remote Amplifier",
"price": "100.0",
"published": false,
"user": {
"id": 1,
"email": "blair.runolfsson@hirthe.info",
"created_at": "2017-08-27T12:13:26.977Z",
"updated_at": "2017-08-27T12:13:26.977Z",
"auth_token": "KA1ugPyXzXsULh6rN6QX",
"product_ids": [
1
]
}
},
{
"id": 2,
# rest of attributes
},
{
"id": 3,
# rest of attributes
},
{
"id": 4,
# rest of attributes
}
},
{
"id": 5,
# rest of attributes
},
{
"id": 6,
# rest of attributes
}
]
手动测试:
我的应用程序似乎工作正常。在点击api.mydomain.dev/products
时,在rails控制台中创建了3个产品之后,我得到了很好的json响应:
{
"products": [
{
"id": 1,
"title": "product",
"price": "3.3",
"published": false,
"user": {
"id": 1,
"email": "example@marketplace.com",
"created_at": "2017-08-13T07:31:29.339Z",
"updated_at": "2017-08-27T13:09:06.948Z",
"auth_token": "HJLb-d4DpgxQDzkR1RDf",
"product_ids": [
1,
2
]
}
},
{
"id": 2,
# attrs of second product in json
},
{
"id": 3,
# attrs of third product in json
}
]
}
还有URL中的参数,如api.mydomain.dev/products?min_price=15和max_price=30
(顺便说一句,此处未包括其余两个失败的规范):
我在spec/models/product_spec.rb中还有4个错误,但为了简短起见,我将不在这里包括它们。我认为原因是一样的,这个错误是最简洁的
我可能会提供更多的代码,但是它在我的github repo上是公开的,例如。
或
总结:
该应用程序工作正常,并在手动测试时提供预期输出。我在自动测试时得到了错误,即使在提交回这些测试通过的地方之后也是如此。这可能表明问题出在系统方面。如果它不在系统端,为什么它得到了额外的两个对象,而它以前得到了四个?问题出在哪里?可能是测试数据库没有被清理。尝试运行rails c test
并查询Product.count
,查看测试数据库是否有可能导致此错误的产品记录。在使用工厂创建记录之前,您还可以在before(:each)块中放入byebug
或put Product.count
,以确保在运行测试之前没有现有记录。如果是这种情况,您需要在测试前后手动或通过DatabaseCleaner Gem清理数据库
提示:
最好对正在创建和期望的记录数使用一个变量。
例如:
是的,就是这样。为了快速检查,正如您所说,我去了railsctest
,这两条记录就在那里(不知道怎么做)。我用产品销毁了它们。销毁所有并且所有五个测试都再次通过。看来我需要深入研究数据库。谢谢师父,很高兴我能帮忙。干杯
{
"products": [
{
"id": 3,
"title": "ps two",
"price": "20.0",
"published": false,
"user": {
"id": 2,
"email": "foo@bar.com",
"created_at": "2017-08-27T12:50:58.905Z",
"updated_at": "2017-08-27T12:50:58.905Z",
"auth_token": "YynJJeyftTEX49KU1-qL",
"product_ids": [
3
]
}
}
]
}
products_count = 4.times { FactoryGirl.create :product }
expect(products_response).to have(products_count).items