代码之家  ›  专栏  ›  技术社区  ›  y0mbo

Cucumber和webrat-如何处理path.rb中的动态URL?

  •  11
  • y0mbo  · 技术社区  · 15 年前

    我在RubyonRails项目中使用Cucumber进行BDD开发,我对path.rb如何处理Rails应用程序中使用的路径感到困惑。

    class Parent < ActiveRecord::Base
      has_many :children
    end
    
    class Child < ActiveRecord::Base
      belongs_to :parent
    end
    

    我有以下特点:

    Scenario: A test feature
        Given I am on the parent page
         When I follow "Link to Children"
         Then I should be on the children list page
    

    路径定义为:

    def path_to(page_name)
      case page_name
      when /the children list page/
           '/parents/:id/children'
    end
    

    我遇到的问题是运行该功能时出现以下错误:

    Spec::Expectations::ExpectationNotMetError: expected: "/parents/:id/children",
     got: "/parents/1726/children" (using ==)
    

    我真的不在乎身份证是什么。我该怎么做呢?对于默认的web步骤,这可能吗?我是否用错误的方式思考这个问题?

    2 回复  |  直到 15 年前
        1
  •  18
  •   jonnii    15 年前

    when /the children list page for "(.+)"/
        p = Parent.find_by_name($1)
        parent_children_path(p)
    
        2
  •  2
  •   Chris    14 年前

    在我们的应用程序中,每当用户单击“新建”按钮时,我们总是希望数据库中有一条新记录。因此,控制器的新操作会自动调用create,然后重定向到edit操作。

    我们在测试中也遇到了类似的问题,当时我们不太关心ID是什么——只是它进入了应用程序的编辑页面。

    这是我想到的。

    (注意:步骤定义是使用

    Then /^(?:|I )should now be editing the (.*)$/ do |model|
      id = find_by_id("#{model}_id").value
      Then "I should be on the edit #{model} page for \"#{id}\""
    end
    

    该步骤找到隐藏字段,从中提取ID,然后查找web_步骤以解析该模型的路径。

    只需确保您有一个与正在查找的模型匹配的路径。

    when /the edit person page for "([^\"]*)"/
      edit_person_path($1)