我有以下型号:
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
我有以下工厂:
Factory.define :user do |user|
user.email { Factory.next :email }
user.association :profile
end
Factory.define :profile do |profile|
profile.name 'Name'
end
所以这是我的特点:
Given a profile: "John" exists with name: "John"
And a user: "John" exists with profile: profile "John"
有什么办法可以改善这一点吗?我希望能够写出这样的东西:
Given a user: "John" exists with a profile: profile "John" exists with name: "John"
它创造了一些类似的东西:
Factory(:user, :profile => Factory(:profile, :name) )
几乎我需要一个嵌套匹配器。你能为此建议一个步骤吗?
或者你能建议一种替代方法来实现这一目标吗?