[Rspec-users] Mocking causes empty specification blocks

David Chelimsky dchelimsky at gmail.com
Mon Jul 24 14:58:38 EDT 2006


On 7/24/06, Chris Roos <chrisjroos at gmail.com> wrote:
> On 7/24/06, David Chelimsky <dchelimsky at gmail.com> wrote:
> > On 7/24/06, Chris Roos <chrisjroos at gmail.com> wrote:
> > > I've found myself mocking out a published api in my set-up method.
> > > This has led to some contexts having a large set-up and empty
> > > specifications.  As the mocks get auto-verified, the specification
> > > blocks have nothing to do but serve as documentation.  Does this sound
> > > bad?
> >
> > What is it that you are specifying? Why are there no statements in the specs?
> >
> I'm specifying the relationship between my object and the mocks.  The
> expectations on the mock object are created in the setup block and
> auto-verified in my empty specification.
>
> I've pasted an example context below.  I'm actually wanting to ensure
> that a validator doesn't get installed.  All the expectations (bar
> :header_row?) are irrelevant in this case; but I don't think I can use
> a null_object mock as I then wouldn't get notified if we try to
> install a validator.
>
> Chris
>
> -- spec --
>
> context "A processed feed validator with one header row but no
> required headers" do
>
>   setup do
>     header_row = mock('header_row')
>     header_row.should_receive(:header_row?).and_return(true)
>     header_row.should_receive(:extend).with(Validatable)
>     header_row.should_receive(:errors)
>     header_row.should_receive(:fields).and_return([[]])
>     io = [header_row]
>     feed_validator = FeedValidator.new(io)
>     feed_validator.process
>   end
>
>   specify "should not install an empty header validator" do
>   end
>
> end

It strikes me that you're mixing setup w/ events. Since your spec is
that "should not install an empty header validator", then put
something in the specify block that indicates when the installation is
happening, and perhaps the specific expectation as well. For example:

context "A processed feed validator with one header row but no
required headers" do

 setup do
   @header_row = mock('header_row')
   @header_row.should_receive(:header_row?).and_return(true)
   @header_row.should_receive(:extend).with(Validatable)
   @header_row.should_receive(:errors)
   @io = [header_row]
   @feed_validator = FeedValidator.new(io)
 end

 specify "should not install an empty header validator" do
   @header_row.should_receive(:fields).and_return([[]])
   @feed_validator.process
 end

end

Also, you can use null_object to tell the mock to not worry about
calls you don't specify. So, for example, you could do this in setup:

   @header_row = mock('header_row', :null_object => true)

and then you don't need this in your setup:

   @header_row.should_receive(:extend).with(Validatable)
   @header_row.should_receive(:errors)

The only things you need in the setup would be the messages that
result in some return value. Then you could have separate specify
blocks for "should supply errors" and "should extend using a
Validitable" (for example).


More information about the Rspec-users mailing list