代码之家  ›  专栏  ›  技术社区  ›  Josiah Kiehl

未定义的方法“add”在黄瓜步骤上通常有效

  •  1
  • Josiah Kiehl  · 技术社区  · 16 年前

    我定义了一条路径:

    when /the admin home\s?page/
      "/admin/"
    

    我有一个假设:

      Scenario: Let admins see the admin homepage
        Given "pojo" is logged in
        And "pojo" is an "admin"
        And I am on the admin home page
        Then I should see "Hi there."
    

    我有一个失败的场景:

      Scenario: Review flagged photo
        Given "pojo" is logged in
        And "pojo" is an "admin"
      ...bunch of steps that create stuff in the database...
        And I am on the admin home page
        Then ... the rest of the steps
    

    在第二个场景中失败的步骤是“我在管理主页上”,这在第一个场景中通过得很好。

    我得到的错误是:

    And I am on the admin home page                 # features/step_definitions/web_steps.rb:18
      undefined method `add' for {}:Hash (NoMethodError)
      ./app/controllers/admin_controller.rb:13:in `index'
      ./app/controllers/admin_controller.rb:11:in `each'
      ./app/controllers/admin_controller.rb:11:in `index'
      /usr/lib/ruby/1.8/benchmark.rb:308:in `realtime'
      ./features/step_definitions/web_steps.rb:19:in `/^(?:|I )am on (.+)$/'
      features/admin.feature:52:in `And I am on the admin home page'
    

    这很奇怪…为什么在第一种情况下是可以的,而在第二种情况下就不行了,因为只有一堆步骤在数据库中创建记录了?

    [编辑]

    下面是向数据库添加内容步骤:

    Given /^there is a "([^\"]*)" with the following:$/ do |model, table|
      model.constantize.create!(table.rows_hash)
    end
    
    1 回复  |  直到 16 年前
        1
  •  0
  •   Josiah Kiehl    16 年前

    不用说,我只是太累了,看不到我自己的错误信息。