在 rspec 测试中发布到不同的控制器 [英] post to a different controller in an rspec test

查看:40
本文介绍了在 rspec 测试中发布到不同的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何发布到与测试脚本当前指向的控制器不同的控制器?

How do I post to a different controller than the one the test script is currently pointing at?

示例:在 user_controller_spec.rb 中

Example: in user_controller_spec.rb

  it "should just post to users" do        
    post :create, @params    # this goes to the users controller
  end

我想做类似的事情:

  it "should post to user and people to do some integration testing" do        
    post :create, @params    # this goes to the users controller still
    post 'people', :create, @params   # this goes to the people controller
  end

ps:我不想设置黄瓜

推荐答案

控制器规范是 Rails 功能测试的包装器,不支持多个请求或控制器.您想要的是 RSpec 请求规范(rails 3)或集成规范(rails 2).这些包装 Rails 集成测试确实支持多个控制器的多个请求(甚至多个会话),但它们的工作方式与控制器规范略有不同.您必须使用完整路径(因此获取 new_thing_path),并且您不能在控制器上存根任何东西(因为在您发出请求之前没有控制器).

Controller specs are wrappers for Rails functional tests, which don't support multiple requests or controllers. What you want is an RSpec request spec (rails 3) or an integration spec (rails 2). These wrap Rails integration tests, which does support multiple requests with multiple controllers (multiple sessions, even), but they work a bit differently from controller specs. You have to use the full path (so get new_thing_path), and you can't stub anything on the controller (because there is no controller before you make a request).

参见 http://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spechttp://api.rubyonrails.org/classes/ActionDispatch/IntegrationTest.html 了解更多信息.

See http://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec and http://api.rubyonrails.org/classes/ActionDispatch/IntegrationTest.html for more info.

这篇关于在 rspec 测试中发布到不同的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆