Using pickle with cucumber and factory_girl to create related models and pass parameters through a nested model

I have the following models:

class User < ActiveRecord::Base
    has_one :profile, :dependent => :destroy
    def before_create
        self.profile ||= Profile.new
    end
end

class Profile < ActiveRecord::Base
  belongs_to :user
  validates_uniqueness_of :name
end

And I have the following factories:

Factory.define :user do |user|
  user.email                 { Factory.next :email }
  user.association           :profile
end

Factory.define :profile do |profile|
  profile.name  'Name'
end

So this is my feature:

Given a profile: "John" exists with name: "John"
And a user: "John" exists with profile: profile "John"

Is there any way to improve this? I would like to write something like this:

Given a user: "John" exists with a profile: profile "John" exists with name: "John"

And he creates something line by line:

Factory(:user, :profile => Factory(:profile, :name) )

Its almost that I need an embedded match. Can you suggest a step for this?

Or can you suggest an alternative way to achieve this?

+3
source share
1 answer

My suggestion is to write your steps in a more declarative style and avoid getting fragile, random details into your scripts.

Here are some links:

+1

Source: https://habr.com/ru/post/1731294/


All Articles