导轨,黄瓜:让对象及其关联 [英] Rails, Cucumber: make object and its associations

查看:215
本文介绍了导轨,黄瓜:让对象及其关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 mongoid ,的机械师2 咸菜。但我认为,这个问题是比较常见的。

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

我有一个帐户模型:

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协会):

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

我觉得这也不是那么好主意,用IDS这样的方式。什么是协会创建对象的最佳实践?我是,如果泡菜不错。例如,可以帮助。

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.

推荐答案

您可以设置这样的蓝图:

You can set up blueprints like this:

User.blueprint do
  name
  # ...
end

Account.blueprint do
  user 
  # ...
end

和里面的黄瓜:

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

这篇关于导轨,黄瓜:让对象及其关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆