[rspec-users] basic noob questions

David Chelimsky dchelimsky at gmail.com
Sat Jul 25 00:52:44 EDT 2009


On Fri, Jul 24, 2009 at 7:13 PM, norm<codehacker at comcast.net> wrote:
> Struggling mightily!
>
> Testing a controller called Govtpositions. Just focusing on testing
> the create method for the moment. I would imagine there should be two
> examples for a successful save... instantiate the model object and
> then save it, is that right?
>
> So just to test the model object instantiation, I have:
>
> describe GovtpositionsController, do
>  before do
>    Govtposition.stub!(:new).and_return(@govtposition)
>  end
>
>  it "should create a new govt position model" do
>    @govtposition = mock_model(Govtposition)
>    @params = {"name"=>"chief"}
>    Govtposition.should_receive(:new).with(@params).and_return
> (@govtposition)
>  end
>
> end
>
> This fails, with the reason that @govtposition is receiving an
> unexpected :save. Well I didn't intend to test save,

Sure, but if the code does that, then you have to account for it. You
can just stub save and not expect it. That's a perfect case for a
method stub - something unimportant to your example, but necessary for
the example to run.

> I was planning to
> write another example for that assertion. Why can't I just test object
> creation in it's own example, must I also test saving in the same
> example? I'm clearly missing something basic!
>
> thanks in advance for any insight you can offer

Assuming you're dealing w/ a standard create action, IMO, you don't
really need to spec the instantiation of the object. This is one of
those gray areas in which you're touching implementation with stubs
and mocks, but you're not really specifying implementation. i.e., the
examples might read:

GovtpositionsController
  POST create
    with valid data
      creates a new govtposition with the submitted data
      redirects to the list of govtpositions

If you're using mocks/stubs for isolation and speed, you may need to
stub :new if that's what the create method is using, but that's just
there to get the example to execute - not because it's part of what's
being specified. Make sense?

Cheers,
David


More information about the rspec-users mailing list