Capybara / Cucumber:名称空间中嵌套资源的RoutingError

时间:2012-03-11 15:27:22

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

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

我定义了以下路线:

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

以及以下黄瓜功能:

@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动作时,一切正常,但每当我运行黄瓜时,我得到:

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模块中,但正如我之前所说,它在手动测试时有效。

1 个答案:

答案 0 :(得分:1)

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

最后,我从Rails 2.3中删除了默认路由并使它们仅在黄瓜内部激活,这似乎有效:

# 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中消失了),但我想把它放在互联网的某个地方给那些来看的人。