如何确定 Rails 控制器的默认操作?

发布于 2024-11-01 03:37:06 字数 916 浏览 0 评论 0原文

我正在进行功能测试,需要断言特定的 XHTML 标签存在于特定的控制器集中。我基本上是这样做的:

class ApplicationControllerTest < ActionController::TestCase
  def setup
    @controller = ApplicationController.new
  end
  # [...]
  def test_foo_bar_and_baz_include_stylesheet
    [FooController, BarController, BazController].each do |controller|
      @controller = controller.new
      get :show
      assert_select 'head > link[rel="stylesheet"]'
    end
  end
end

问题是并非所有控制器都有 :show 操作。我需要做的是向控制器或路由配置询问控制器的默认操作,然后调用 get

更新: Joseph Silvashy 是对的:我最终分离了我的控制器 get 分成单独的测试用例。解决“默认”路由问题已经够糟糕的了,直到我发现我的一些路由附加了条件,并且我必须解析它们才能生成正确的 get 调用。最后,Rails 功能测试不能很好地处理在同一测试用例中多次调用 get 的情况,尤其是当这些调用命中多个控制器时。 :(

我认为这里的教训是我们都熟记在心但有时很难接受的教训:如果代码看起来很毛茸茸,那么你可能做错了。;)

I'm working on a functional test that needs to assert that a certain XHTML tag is present in a certain set of controllers. I'm basically doing this:

class ApplicationControllerTest < ActionController::TestCase
  def setup
    @controller = ApplicationController.new
  end
  # [...]
  def test_foo_bar_and_baz_include_stylesheet
    [FooController, BarController, BazController].each do |controller|
      @controller = controller.new
      get :show
      assert_select 'head > link[rel="stylesheet"]'
    end
  end
end

The problem is that not all controllers have a :show action. What I need to do is ask either the controller or the routing configuration for the controller's default action and call get with that.

Update: Joseph Silvashy was right: I ended up separating my controller gets out into separate test cases. Solving the "default" route problem was bad enough, until I discovered that some of my routes had conditions attached, and I would have to parse them to craft the correct get call. And finally, Rails functional tests don't deal very well with calling get multiple times in the same test case, especially when that those calls are hitting multiple controllers. :(

I think the lesson here is one that we all know by heart but sometimes is hard to accept: if the code looks hairy, you're probably doing it wrong. ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

葵雨 2024-11-08 03:37:06

控制器没有“默认”视图或操作,此外操作可以命名为您想要的任何名称,它们不必是标准的 indexshow、< code>new 等...

我可能必须为要测试的每个控制器:get 执行适当的操作。无论如何,每个测试很可能会有所不同,尽管现在它们都有相同的要求,但我认为无论如何为每个操作编写一个测试都是有意义的。

Controllers don't have a "default" view or action, additionally actions can be named anything you want, they don't have to be the standard index, show, new, etc...

I'll probably have to :get the appropriate action for each controller you want to test. It's likely each test will be different down the road anyhow, even though right now they all have the same requirement, I think it makes sense to write one for each action regardless.

后知后觉 2024-11-08 03:37:06

尝试使用respond_to函数:http://www.ruby-doc。 org/core/classes/Object.html#M001005
检查方法是否存在。

try to use the respond_to function: http://www.ruby-doc.org/core/classes/Object.html#M001005
to check if a method exitst.

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