在 rspec 测试中发布到不同的控制器
如何发布到与测试脚本当前指向的控制器不同的控制器?
例子: 在 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:我不想设置黄瓜
How do I post to a different controller than the one the test script is currently pointing at?
Example:
in user_controller_spec.rb
it "should just post to users" do
post :create, @params # this goes to the users controller
end
I want to do something like:
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: i don't want to setup cucumber
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
控制器规范是 Rails 功能测试的包装器,不支持多个请求或控制器。您想要的是 RSpec 请求规范 (rails 3) 或集成规范 (rails 2)。这些包装 Rails 集成测试,确实支持多个控制器的多个请求(甚至多个会话),但它们的工作方式与控制器规范略有不同。您必须使用完整路径(因此获取 new_thing_path),并且不能在控制器上存根任何内容(因为在发出请求之前没有控制器)。
请参阅http://relishapp.com/rspec/rspec-rails/ docs/request-specs/request-spec 和 http://api.rubyonrails.org/classes/ActionDispatch/IntegrationTest.html 了解更多信息。
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).
See http://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec and http://api.rubyonrails.org/classes/ActionDispatch/IntegrationTest.html for more info.
有一种方法可以在调用测试方法之前分配 @controller 的值。例子
There is a way if you assigning the value of @controller before call test method. Example
基于其他答案,但更安全。
存储当前控制器实例并使用所需的控制器创建一个新实例。最后,用旧的存储实例替换新的控制器实例。
Based on other answer, but more safe.
Store current controller instance and create a new with the required controller. Finally, replace new controller instance with the old stored instance.