在 rspec 测试中发布到不同的控制器

发布于 2024-12-02 21:23:32 字数 524 浏览 0 评论 0原文

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

例子: 在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初与友歌 2024-12-09 21:23:33

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

请参阅http://relishapp.com/rspec/rspec-rails/ docs/request-specs/request-spechttp://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.

半枫 2024-12-09 21:23:33

有一种方法可以在调用测试方法之前分配 @controller 的值。例子

def setup

  @controller = UserController.New

  do user stuff

  @controller = ThisController.New

  do test intented for this controller
end

There is a way if you assigning the value of @controller before call test method. Example

def setup

  @controller = UserController.New

  do user stuff

  @controller = ThisController.New

  do test intented for this controller
end
南街女流氓 2024-12-09 21:23:33

基于其他答案,但更安全。

存储当前控制器实例并使用所需的控制器创建一个新实例。最后,用旧的存储实例替换新的控制器实例。

def setup
  old_controller = @controller
  @controller = UserController.new

  # do user stuff

  @controller = old_controller
end

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.

def setup
  old_controller = @controller
  @controller = UserController.new

  # do user stuff

  @controller = old_controller
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文