【问题标题】:Cucumber testing - getting RoutingError黄瓜测试 - 获取 RoutingError
【发布时间】:2011-09-18 17:37:55
【问题描述】:

怎么了,伙计们。请帮忙。
当我运行我的黄瓜测试时,我遇到了这个错误:

No route matches {:action=>"show", :controller=>"accounts"} (ActionController::RoutingError)
      ./features/support/paths.rb:40:in `path_to'

rake 路线显示:

account GET    /accounts/:id(.:format)        {:action=>"show", :controller=>"accounts"}

cucumber_test.feature

   Scenario:
    Given...
    And...
    Then i should be on Show page

features/support/paths.rb

when /^Show page$/
      account_path @account

routes.rb

Myapp::Application.routes.draw do  
resources :accounts  

【问题讨论】:

  • @account.inspect 显示什么?
  • 安迪,我如何将@account.inspect 放入我正在运行的黄瓜测试中?
  • 在When步骤的第一行添加“puts @account.inspect”。
  • 是的,伙计们。你是对的,它返回 nil。现在我要打破我的大脑,因为真的不知道为什么它是 nil :(
  • 嗯,它更像是:你为什么不希望它为零??

标签: ruby-on-rails cucumber bdd


【解决方案1】:

恕我直言,您以某种方式设置了 @account 变量是错误的。

以下是一些可能的方法。你可以使用:

when /the account page/
account_path(Account.first)

或者更好、更干净、更可重用(我不知道您的 Account 架构,所以我使用了通用的“名称”):

when /the account page for account named ".*"/
        account_name = page_name.scan(/".*"/).first.gsub("\"", '') 
        account = Account.find_by_name(account_name)
        account_path(account)

当然,只要您像这样定义“我在”网页步骤:

Given /^(?:|I )am on (.+)$/ do |page_name|
  visit path_to(page_name)
end

【讨论】:

  • 非常感谢。我会用你的好方法。
猜你喜欢
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-25
相关资源
最近更新 更多