views:

437

answers:

0

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 a way I can improve this? I would like to be able to write something like this:

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

And it creates something along the lines of:

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

Its almost that I need a nested matcher. Can you suggest a step for this?

Or can you suggest an alternative way to achieve this?