简体   繁体   English

路线在应用程序中有效,在 Cucumber 测试中失败

[英]Route works in application, fails in Cucumber test

I have a route that works (recognized, can follow it) in a Rails 3 application, but fails in the Cucumber test:我有一条在 Rails 3 应用程序中有效(已识别,可以遵循)的路线,但在 Cucumber 测试中失败:

routes.rb:路线.rb:

resources :tests do
  member do
    post 'start'
  end
end

Working link in application:申请中的工作链接:

=button_to 'Start', start_test_path(@test)

Gherkin step:小黄瓜步骤:

And I am on the test start page for "Sample Test"

Failing in paths.rb在paths.rb中失败

when /the test start page for "([^\"]*)"/
  start_test_path(Test.find_by_name($1))

...

(::) failed steps (::)

No route matches "/tests/1/start" (ActionController::RoutingError)
<internal:prelude>:10:in `synchronize'
./features/step_definitions/web_steps.rb:30:in `/^(?:|I )am on (.+)$/'
features/test_workflow.feature:24:in `And I am on the test start page for "Sample Test"'

Any ideas?有任何想法吗? Thanks!谢谢!

The reason is that 'start' is a post action, not a get action.原因是“开始”是post操作,而不是get操作。 When you use the I am on the... cucumber step you are generating a get request, not a post request.当您使用I am on the... cucumber 步骤时,您正在生成获取请求,而不是发布请求。 To fix this, simply press the button in Cucumber, instead of visiting the direct path.要解决此问题,只需按下 Cucumber 中的按钮,而不是访问直接路径。 This can be done with the press cucumber step, like this:这可以通过press cucumber 步骤来完成,如下所示:

When I press "Start"

This step will generate the appropriate post request, but it must be called after you visit the page that the button is located on.此步骤将生成适当的发布请求,但必须在您访问按钮所在的页面后调用。 Alternatively, you can generate a post request in a custom cucumber step by doing something like this:或者,您可以通过执行以下操作在自定义 cucumber 步骤中生成发布请求:

When /^I am on the test start page$/ do
  post("/tests/1/start")
end

I don't know cucumber, but try this:我不知道 cucumber,但试试这个:

Rename your route from test to tests and your controller from TestController to TestsController将您的路线从test重命名为tests ,并将您的 controller 从TestController重命名为TestsController

This just might be a case where your naming conventions are messing things up, because they are not what Rails expects.这可能是您的命名约定搞砸了的情况,因为它们不是 Rails 所期望的。

I don't know if I am right - just a guess.我不知道我是否正确——只是猜测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM