测试与黄瓜的关联

发布于 2024-08-09 02:03:58 字数 575 浏览 3 评论 0原文

我是 Rails 和 Cucumber 的新手,我正在尝试测试以下场景

Background:
  Given I have a Group named Group 1
  And I go to the list of groups
  And I have the following users records
    | name    | description | group_id |
    | user 1   |            | 1        |
    | user 2   |            | 1        |
  When I follow Details for Group 1

Scenario: List users from group
  Then I should see "user 1"
  And I should see "user 2"

因此,在用户控制器的索引操作中,我列出了 group_id 中的所有用户,但我不知道如何使用 Cucumber 进行测试,因为每次运行测试时,名为 Group 1 的组都有不同的 ID。

有谁知道如何解决这个问题?

谢谢

I'm new to rails and cucumber and I'm trying to test the following scenario

Background:
  Given I have a Group named Group 1
  And I go to the list of groups
  And I have the following users records
    | name    | description | group_id |
    | user 1   |            | 1        |
    | user 2   |            | 1        |
  When I follow Details for Group 1

Scenario: List users from group
  Then I should see "user 1"
  And I should see "user 2"

So, in the index action of my users controller I list all the users from the group_id, but I don't know how to test this using cucumber, because every time I run the test my group named Group 1 has a different id.

Does anyone know how to solve this?

Thanx

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

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

发布评论

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

评论(3

只是在用心讲痛 2024-08-16 02:03:58

所以我找到了一种简单的方法来测试关联。

如果我说我在场景中只有一个组,那么当我执行该步骤时,我可以创建具有特定 id 的组,如下所示

g = Group.create(:name => "Group 1", :id => 1)

然后我只需测试我的页面显示具有 group_id = 1 的用户并且不要不显示具有 group_id <> 的用户1.

就这么简单!!!

So I found out an easy way to test associations.

If I say that a I have only one group in scenario, when I am on that step I can create the group with the specific id like this

g = Group.create(:name => "Group 1", :id => 1)

Then I just have to test the my page shows the users that has a group_id = 1 and don't show the users that has the group_id <> 1.

Easy as that!!!

残龙傲雪 2024-08-16 02:03:58

我会将 ID 换成 group_name

Background:
  Given I have a group called "Ruby users"
  And I go to the list of groups
  And I have the following users records
    | name    | description | group_name |
    | "user 1"   |            | "Ruby users" |
    | "user 2"   |            | "Ruby users" |
  When I follow Details for Group "Ruby users"

Scenario: List users from group "Ruby users"
  Then I should see "user 1"
  And I should see "user 2"

I would exchange the ID for group_name

Background:
  Given I have a group called "Ruby users"
  And I go to the list of groups
  And I have the following users records
    | name    | description | group_name |
    | "user 1"   |            | "Ruby users" |
    | "user 2"   |            | "Ruby users" |
  When I follow Details for Group "Ruby users"

Scenario: List users from group "Ruby users"
  Then I should see "user 1"
  And I should see "user 2"
黯然#的苍凉 2024-08-16 02:03:58

避免使用 ID,而是列出组的名称,因为它永远不会改变。

Avoid using the ID, instead list the name of the group as that will never change.

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