【问题标题】:Capybara/Cucumber : RoutingError for nested resources in a namespaceCapybara/Cucumber:命名空间中嵌套资源的 RoutingError
【发布时间】:2012-03-11 15:27:22
【问题描述】:

我在 Rails 3.2 上使用 Capybara/Cucumber,但遇到了奇怪的路由错误。

我定义了以下路线:

  #routes.rb
  namespace :super_user do
    ...
    resources :events do
      resources :invites
    end
  end
  ...
  resources :invites

以及以下 Cucumber 功能:

@in_progress @current
Scenario: I can invite a USER by email
    Given the following event exists:
    | Name          |
    | The Event     |
    And I go to the event page for "The Event"
    And I follow "Invite new user"
    And I fill in "invite_email" with "user@domain.com"
    ...

活动页面 (EventsController#show) 包含指向 invites#new 操作的链接:

#app/views/super_user/events/show.html.erb
...
<%= content_for :button_bar do %>
  <%= link_to( 'Invite new user', new_super_user_event_invite_path(@event) ) %>
<% end %>

当我手动测试 /super_user/events/1 操作时,一切正常,但每当我运行 cucumber 时,我都会得到:

And I follow "Invite new user" # features/step_definitions/web_steps.rb:45
  uninitialized constant SuperUser::InvitesController (ActionController::RoutingError)
  (eval):2:in `click_link'
  ./features/step_definitions/web_steps.rb:46:in `/^(?:|I )follow "([^"]*)"$/'
  features/create_casino_super_user.feature:24:in `And I follow "Invite new user"'

为什么使用 Cucumber/Capybara 时路由的行为会有所不同?我该如何修复此功能?

bundle list的相关部分:

* cucumber (1.0.6)
* cucumber-rails (1.0.2)
* capybara (1.0.1)
* capybara-webkit (0.6.1 dfa0624)
* rails (3.2.1)

编辑

旁注:InvitesController 类不在 SuperUser 模块中,但正如我之前所说,它在手动测试时有效。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 cucumber capybara


    【解决方案1】:

    我使用的是 Rails 3(不是 3.2),来自 2.3,只是跳到新的路由 DSL。我遇到了一个非常相似的问题,我们的命名空间中的资源路由在直接命中时有效,但不是从 Cucumber/Capybara 内部。

    最后,我从 Rails 2.3 中提取了默认路由,并让它们只在 cucumber 内部活动,这似乎有效:

    # Cucumber doesn't understand the Rails 3 default route, above, so use the old way to make that work
    # TODO remove this when we can/must, and hope that Cucumber is smarter by then
    if File.basename($0) == "cucumber"
        map.connect ':controller/:action/:id.:format'
        map.connect ':controller/:action/:id'
    end
    

    不确定这是否适合您(map.connect 是旧 API 的一部分,我认为它在 3.1 中消失了),但我想把它放在互联网上的某个地方,供那些前来寻找的人使用。

    【讨论】:

    • 感谢您的回答 (+1)。正如您所说,map.connect 现在已弃用。我找到的唯一解决方案是创建一个基本控制器 (InvitesControllerBase) 并在命名空间 SuperUser::InvitesController 类中扩展它。
    猜你喜欢
    • 1970-01-01
    • 2011-02-12
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    相关资源
    最近更新 更多