In the example that is failing, “should build 3 new Products” you have:
it "should build 3 new Products" do
# I have to tag this pending, this results in a weird error I
don’t have time for yet
# full stack testing with cucumber succeeds, human testing does
too
# pending
Product.should_receive(:new)
do_save
Product.count.should == 3
end
You are setting up an expectation with “should_receive(:new)”, and
you’re not supplying anything to be returned so it is defaulting to
nil. This is why you get the NoMethodError calling nil.save… because
your local variable product is nil for the duration of this example.
I don’t think you want to be using mocking down in the model spec like
you are doing. For example, does it really matter that #foreach was
called? Or that Product#new was called? Or that product#save was
called? These can all be inferred if you have a CSV file, save the
import ,and then suddenly you have products. No need to worry about
them directly IMO.
Using mocks in this example happen to make this spec brittle, and it
doesn’t add any value. For example, say you changed from using
fastercsv to some other csv library that had a different API, but it
properly parsed the CSV files. All of the behaviour of importing
products the CSV file still worked, but suddenly your examples fail
because #foreach isn’t be used. When focusing on the Import model
focus on the behaviour you want out of it. Based on the pastie you
made it seems like you want it to import products from a CSV after
save. So, you can write an example for ensuring the right number of
products were created. You might also want to write an example to
ensure the write fields from the CSV ended in the right attributes on
a product. Picking one or two products should be enough to gain
confidence.
I’ve done a quick edit of your original spec to show what I was
thinking when I quickly read your spec: http://pastie.org/693775
Hopefully this helps!
On Wed, Nov 11, 2009 at 8:48 AM, Ray K. [email protected] wrote:
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users
–
Zach D.
http://www.continuousthinking.com (personal)
http://www.mutuallyhuman.com (hire me)
http://ideafoundry.info/behavior-driven-development (first rate BDD
training)
@zachdennis (twitter)