[rspec-users] Model spec for file upload with paperclip and fastercsv

Zach Dennis zach.dennis at gmail.com
Wed Nov 11 10:56:10 EST 2009

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.count.should == 3

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

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. <lists at ruby-forum.com> wrote:
> Now I have another weird error. My Products doesn't save in the specs,
> but it does in cucumber tests and human tests.
> I'd be glad if someone could look into this.
> http://pastie.org/693611
> Ray
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Zach Dennis
http://www.continuousthinking.com (personal)
http://www.mutuallyhuman.com (hire me)
http://ideafoundry.info/behavior-driven-development (first rate BDD training)
@zachdennis (twitter)

More information about the rspec-users mailing list