Rails, Cucumber: make object and its associations

99封情书 提交于 2020-01-07 03:42:08

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/4281386/rails-cucumber-make-object-and-its-associations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!