使用黄瓜和factory_girl的pickle创建关联模型并将参数传递给嵌套模型

时间:2010-02-03 23:54:06

标签: ruby-on-rails cucumber pickle matcher

我有以下型号:

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) )

几乎我需要一个嵌套的匹配器。你能为此建议一步吗?

或者你能否提出另一种方法来实现这一目标?

1 个答案:

答案 0 :(得分:1)

我的建议是以更具说明性的方式编写您的步骤,并避免在您的方案中添加脆弱的附带细节。

以下是一些参考资料: