Anlayışımdan, bir fabrikanın 'to_create' yönteminden dönüş değeri göz ardı edilir. Bu, fabrikanın 'build' veya 'initialize_with' kısmından döndürülen nesnenin, bir testin içinde 'create' çağrılırken nihayetinde döndürülen nesne olduğu anlamına gelir.FactoryGirl to_create dönüş değeri
Benim durumumda, Depo Kalıbının bir varyantını kullanıyorum. Bir depoya 'kaydetme' yöntemine bir çağrı eklemek için fabrikanın 'to_create' bölümünü geçersiz kıldım. Bu yöntem, verilen nesneyi değiştirmez, ancak orijinalin kalıcı biçimini temsil eden bir nesneyi döndürür. Bununla birlikte, 'yapı' bloğundan döndürülen örnek fabrikadan döndürülür ve 'to_create' bloğunda oluşturulan örnek değil. Benim kodumda, bu, "eylemsiz" formunun, kurtarıcı eylemden güncellenmiş özniteliklere (ör. 'Id') sahip nesneye değil, döndürülmesi anlamına gelir.
'create' bloğunun dönüş değeri 'to_create' bloğunun sonucu veya bu blokta oluşturulan bir değer olarak zorlamak için bir yol var mı?
class Foo
attr_accessor :id, :name
...
end
class FooRepository
def self.create(name)
Foo.new(name) # this object is not yet persisted and has no .id
end
def self.save(foo)
# this method must not guarantee that the original Foo instance
# will always be returned
...
updated_foo # this is a duplicate of the original object
end
...
end
FactoryGirl.define do
factory :foo, class: FooRepository do
# create an example Foo
initialize_with { FooRepository.create(name: "Example") }
# save the Foo to the datastore, returning what may be a duplicate
to_create {|instance| FooRepository.save(instance)}
end
end
describe FooRepository do
it "saves the given Foo to the datastore" do
foo = create(:foo)
foo.id #=> nil
...
end
end
Güzel! Ama yine de çok güzel bir cevaba yardım etti! :) – Aleks