创建带有对象 ID 的路径以与 Cucumber 场景映射

发布于 2024-12-11 00:40:02 字数 671 浏览 0 评论 0原文

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

一般路径类似于:/posting/id/edit
即 /posting/11/edit

这是我的发布。功能场景

# 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."

我涉足了一些 Factory Girl 的东西,但我不具备正确使用它的知识(如果它提供了解决方案),并且无法找到一个相关的例子。 我也看到了很多关于“Pickle”的建议,但如果可能的话,我想避免这条路线,以使事情变得简单,因为我的经验非常有限。

谢谢!

I'm trying to create a cucumber scenario that checks to see if elements are loaded for an 'edit posting' page. My trouble, however, is that I don't know how to create a path that will direct it to the page.

The general path is something like: /posting/id/edit
i.e. /posting/11/edit

Here is my posting.feature scenario

# 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."

I dabbled around with some Factory Girl stuff, but I don't have the knowledge to use it appropriately (if it offers a solution), and wasn't able to find a relevant example.
I've also seen a lot of suggestions with regards to 'Pickle', but if possible I'd like to avoid that route to keep things simple seeing as I have very limited experience.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天暗了我发光 2024-12-18 00:40:02

您的网站上是否有一个链接可以将某人带到编辑页面?然后您可以执行以下操作:

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

假设您的主页上有一个链接,其文本为“帖子”,然后结果页面中存在另一个名为“编辑”的链接。这是实现此目的的最佳方法,因为应该有一个直接路由到您正在测试的任何页面。 web_steps.rb 中也提供了这些步骤,

您也可以像使用 Given I am on the edit posts page 那样进行自定义步骤,代码如下所示:

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

您当然也可以概括为我在编辑帖子页面上发布 11。但总的来说,黄瓜测试是验收测试,这意味着不能绕过这样的事情。您应该有一个可以单击的编辑页面的链接。

Is there a link on your website that would take someone to the edit page? Then you could do something like:

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

This assumes that there is a link on your homepage whose text is Posts, and then another one in the resulting page called Edit. This is the best way to accomplish this, because there should be a direct route to whatever page you are testing. Those steps are also provided in web_steps.rb

You could also make a custom step like you did there with Given I am on the edit posting page and the code would be something like:

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

Which you of course could also generalize like I am on the edit posting page for posting 11. But in general, cucumber tests are acceptance tests, which means not bypassing things like this. You should have a link to the edit page that can be clicked.

尽揽少女心 2024-12-18 00:40:02

我想出了一个解决方案,但我不确定它的有效性和清洁程度。我最终使用了 Factory Girl(安装了 gem)。
我保持我的场景不变。

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

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

features/support 下,我创建了一个文件 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.rbFactory (:posting) 基本上是 Factory.create(:posting)),它又使用我在 factories.rb 中创建的工厂定义。这会导致创建一个发布实例。

然后在我的paths.rb

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

从实例中传递 id,最终获得类似于 /posting/1/edit 的路径,然后测试继续进行!

如果有任何需要纠正的地方,请告诉我,因为我只是在学习诀窍。
希望这会对其他新手有所帮助!

I came up with a solution, but I am not sure of its validity in terms of how clean it is. I ended up using Factory Girl (installed the gem).
I kept my scenario the same.

Under features/step_definitions I created posting_steps.rb

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

Under features/support I created a file factories.rb with the following inside:

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

In my paths.rb I used

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

How it works (or at least how I think it works) is that as

Given there is a posting 

is executed, the posting_step.rb is invoked (Factory(:posting) is basically Factory.create(:posting)), which in turn uses the factory definition I created in factories.rb. This leads to an instance of a posting being created.

Then in my paths.rb

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

gets passed the id from the instance, to ultimately get a path that could resemble /posting/1/edit , and the test continues on its way!

If there are any corrections to be made, please let me know as I am just learning the ropes.
Hopefully this will help other newbies out there!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文