Rails、Cucumber:创建对象及其关联

发布于 2024-10-04 15:37:41 字数 867 浏览 3 评论 0原文

我正在使用 mongoid机械师 2pickle。但我认为,这个问题更常见。

我有一个帐户模型:

class Account
  include Mongoid::Document
  include Mongoid::Timestamps

  referenced_in   :user
end

和用户:

class User
  include Mongoid::Document
  include Mongoid::Timestamps

  references_one :account
end

我有以下场景(我设置了reference_one关联):

  Scenario: Client views his account
    Given a user with id: "4ceede9b5e6f991aef000007"
    And the following accounts exist:
      | user_id                        |
      |  4ceede9b5e6f991aef000007      |
         .....

我认为这样使用ids并不是一个好主意。创建具有关联的对象的最佳实践是什么?如果泡菜的话我会很好。例如,可以提供帮助。

I'm using mongoid, machinist 2 and pickle. But I think, that question is more common.

I have an Account model:

class Account
  include Mongoid::Document
  include Mongoid::Timestamps

  referenced_in   :user
end

and User:

class User
  include Mongoid::Document
  include Mongoid::Timestamps

  references_one :account
end

I have the following scenario(I set reference_one association):

  Scenario: Client views his account
    Given a user with id: "4ceede9b5e6f991aef000007"
    And the following accounts exist:
      | user_id                        |
      |  4ceede9b5e6f991aef000007      |
         .....

I think this is not so good idea use ids such way. What is the best practice of creating object with associations? I would be nice if pickle. for example, could help.

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

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

发布评论

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

评论(1

梦开始←不甜 2024-10-11 15:37:41

您可以像这样设置蓝图:

User.blueprint do
  name
  # ...
end

Account.blueprint do
  user 
  # ...
end

在 cucumber 内部:

Given the following accounts exist
  | user  |
  | Fred  | 
  | Ethel |

如果 pickle 不处理上面的步骤,您可以创建自己的步骤定义,如下所示:

Given /^the following accounts:$/ do |class_name, table|
  table.hashes.each do |attributes|
    u = User.make! :name => attributes[:user]
    Account.make! :user => u
  end
end

You can set up blueprints like this:

User.blueprint do
  name
  # ...
end

Account.blueprint do
  user 
  # ...
end

And inside cucumber:

Given the following accounts exist
  | user  |
  | Fred  | 
  | Ethel |

If pickle doesn't handle the step above, you can create your own step definition like this:

Given /^the following accounts:$/ do |class_name, table|
  table.hashes.each do |attributes|
    u = User.make! :name => attributes[:user]
    Account.make! :user => u
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文