3

私は次のモデルを持っています:

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

ネストされたマッチャーが必要です。このためのステップを提案できますか?

または、これを達成する別の方法を提案できますか?

4

1 に答える 1

1

私の提案は、より宣言的なスタイルでステップを記述し、脆弱な付随的な詳細をシナリオに入れないようにすることです。

ここにいくつかの参照があります:

于 2012-01-06T17:02:00.163 に答える