Rails, Cucumber: make object and its associations
-
28-09-2019 - |
Question
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.
Solution
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow