创建具有对象ID的路径以使用黄瓜方案进行映射

时间:2011-10-19 18:20:50

标签: ruby-on-rails cucumber

我正在尝试创建一个黄瓜场景,检查是否为“编辑发布”页面加载了元素。但是,我的麻烦在于我不知道如何创建一个将它引导到页面的路径。

一般路径如下:/ posts / id / edit
即/发布/ 11 /编辑

这是我的posts.feature场景

# Editing existing post 
Scenario: Saving the edits to an existing post
    Given I am logged in 
    Given there is a posting
    Given I am on the edit posting page
    When I fill in "posting_title" with "blah"
    And I fill in "posting_location" with "blegh"
    When I press "Update posting"
    Then I should see "Posting was successfully updated."

我涉及一些工厂女孩的东西,但我没有适当使用它的知识(如果它提供了解决方案),并且无法找到相关的例子。 我也看到了很多关于'泡菜'的建议,但是如果可能的话,我想避免这样的路线让事情变得简单,因为我的经验非常有限。

谢谢!< / p>

2 个答案:

答案 0 :(得分:1)

您的网站上是否有链接可以将某人带到编辑页面?然后你可以做类似的事情:

Given I am on the homepage
And I follow "Posts"
And I follow "Edit"

这假设您的主页上有一个链接,其文本为Posts,然后在结果页面中有另一个名为Edit的链接。这是实现此目标的最佳方法,因为应该有直接路由到您正在测试的任何页面。 web_steps.rb

中也提供了这些步骤

您也可以使用Given I am on the edit posting page进行自定义步骤,代码如下:

Given /^I am on the edit posting page$/ do
    visit("/posting/11/edit")
end

你当然也可以概括为I am on the edit posting page for posting 11。但总的来说,黄瓜测试是验收测试,这意味着不要绕过这样的事情。您应该有一个指向可以单击的编辑页面的链接。

答案 1 :(得分:0)

我想出了一个解决方案,但我不确定它在多么干净方面的有效性。我最终使用了Factory Girl(安装了gem)。 我保持我的情景一样。


features / step_definitions 下,我创建了 posting_steps.rb

Given /^there is a posting$/ do
    Factory(:posting)
end


功能/支持下,我创建了一个文件 factories.rb ,其中包含以下内容:

Factory.define :posting do |f|
  f.association :user
  f.title 'blah'
  f.location 'Some place'
end

在我的 paths.rb 中,我使用了

when /the edit posting page/
    edit_posting_path(Posting.first)



它是如何工作的(或者至少我认为它是如何工作的)就是作为

Given there is a posting 

被执行, posting_step.rb 被调用( 工厂(:发布)基本上是Factory.create(:发布) ),它又使用我在 factories.rb 中创建的工厂定义。这导致创建发布的实例。

然后在我的 paths.rb

when /the edit posting page/
    edit_posting_path(Posting.first)

从实例传递id,最终得到一个类似/发布/ 1 /编辑的路径,测试继续进行!

如果有任何更正,请告诉我,因为我正在学习绳索。 希望这会帮助其他新手!