Here's a combined approach that combines the best of @antiqe and @Fitzsimmons answers. However, this is much more detailed.
The idea is to make fun of Foo.create in a way that behaves like AR :: Base.create. Frist, we define a helper class:
class Creator def initialize(stub) @stub = stub end def create(attributes={}, &blk) attributes.each do |attr, value| @stub.public_send("#{attr}=", value) end blk.call @stub if blk @stub end end
And then we can use it in our specifications:
it "sets the description" do f = stub_model(Foo) stub_const("Foo", Creator.new(f)) Something.method_to_test f.description.should == "thing" end
You can also use FactoryGirl.build_stubbed instead of stub_model . However, you cannot use mock_model , mock or double , since you will have the same problem again.
Now your spec will pass any of the following code snippets:
Foo.create(description: "thing") Foo.create do |foo| foo.descrption = "thing" end foo = Foo.create foo.descrption = "thing"
Feedback is welcome!
source share