在 Rails 集成规范中向同一控制器发出两个请求
我在 Rails 集成测试中使用 rspec 向同一个 url 发出两个请求时遇到问题
it 'does something' do
# get '/something', {:status=>'any_other'}, @header ## <<<<< this line causes problem!
get '/something', {:status=>'ok'}, @header
doc = Nokogiri::HTML(response.body)
lis = doc.css('#the_id')
lis.size.should == 1
lis[0].text.should include('anything')
end
如果我向同一个控制器发出两个请求,则测试似乎会保持旧的响应...
在上面的示例中,如果我取消注释行,测试中断,因为它维护第一个“查询”的结果,
这是测试堆栈的限制,还是我做错了什么?
I'm having problem making two requests to the same url in a rails integration test, with rspec
it 'does something' do
# get '/something', {:status=>'any_other'}, @header ## <<<<< this line causes problem!
get '/something', {:status=>'ok'}, @header
doc = Nokogiri::HTML(response.body)
lis = doc.css('#the_id')
lis.size.should == 1
lis[0].text.should include('anything')
end
If I make two requests to the same controller, the test seems to maintain the old response...
In the above example, if I uncomment that line, the test breaks beacause it maintains the result of the first 'query'
Is it a limitation of the test stack, or am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于普通的旧 Rails 测试套件,功能测试适用于单个请求,如果您想测试流程,您应该使用集成测试(您可以在功能测试中重置控制器)。
rspec-rails 的控制器规范继承自 Rails 功能测试,因此它们具有相同的限制。您可以将 rspec 与 capybara 或 webrat (我推荐前者)一起使用进行集成测试。
此外,最新版本的 rspec-rails 具有“请求规范”,其中“混合了 Rails 集成测试的行为”: https ://github.com/rspec/rspec-rails
With plain old Rails test suite, functional tests are for single request and if you want to test flows you should use integration tests (you can reset the controller in functional tests).
Controller specs from rspec-rails inherit from Rails functional tests, so they have same limitation. You can use rspec with capybara or webrat (I recommend the former) for integration tests.
Also, recent versions of rspec-rails has "request specs" which "mix in behaivour of Rails integration tests": https://github.com/rspec/rspec-rails
应该编写 Rails 集成测试,以便在案例中测试单个请求 - 响应周期。我们可以检查重定向。但如果你必须做类似
get '/something', {:status=>'any_other'}, @header
get '/something', {:status=>'ok'}, @header
你应该写两个对此有不同的情况。
rails integration tests should be written so that the on case tests the one single request - response cycle. we can check redirects. but if you have to do something like
get '/something', {:status=>'any_other'}, @header
get '/something', {:status=>'ok'}, @header
You should write two different cases for this.
使用 Capybara 而不是 rspec 是(请求)集成测试的更好解决方案。它使用与 rspec 相同的语法,并允许在单个 it 块中进行多个请求。我使用 rspec 进行单元测试,使用 capybara 进行集成测试。
https://github.com/jnicklas/capybara
Using Capybara instead of rspec is a better solution for (request) intergration tests. It uses the same syntax as rspec and allow multiple requests in a single it block. I use rspec for unit testing and capybara for integration testing.
https://github.com/jnicklas/capybara
您必须清除实例变量,或者可能是唯一必要的变量。
假设您在控制器中使用
@book
。如果您使用
inherit_resources
You have to clear your instance variables, or maybe the only necessary one.
Let's pretend you use
@book
in your controller.If you are using
inherit_resources
我不认为问题在于您得到相同的响应,而是您实际上发送了相同的请求两次。
当我想在同一个测试中提出两个请求时,以下内容对我有用:
@request.delete_header 'RAW_POST_DATA'
I don't think the problem is that you are getting the same response, it is that you are actually sending the same request twice.
The following works for me when I want to make two requests in the same test:
@request.delete_header 'RAW_POST_DATA'