Re: Re: Single assert / Single mock rule in practice

Avi Kessner <[email protected]> Tue, 14 Jan 2014 18:31:46 +0200
Newsgroups gmane.comp.programming.test-driven-development
Message-ID <CAGEhWpEg9CrKiCCS2nqx90m77SibFR6oH7umJX3P_o_3kX2xEg@mail.gmail.com>
"I could have replaced
self.assertTrue(item_exists)
with
foo.xdep.get_item.assert_called_once_with(1)"

These two things are not equivalent tests.  your first assert, is correct
and is testing the interface.  You expect the item to exist if get_item
returns the value of 1.
Your second assertion is testing the implementation.  What if you later
change the get_item function to return an object instead of the item id?
 Do you really want to re-write this test?

brought to you by the letters A, V, and I
and the number 47


On Tue, Jan 14, 2014 at 6:18 PM, Mateusz Łoskot <[email protected]> wrote:

>
>
> On 14 January 2014 12:05, Mateusz Łoskot <[email protected]> wrote:
> > 2) What about having both assertions, would that break the single
> > assertion rule?
> >
> > self.assertTrue(item_exists)
> > foo.xdep.get_item.assert_called_once_with(1)
> > [...]
>
>
> > 3) AFAIU, it's natural that unit tests are coupled with implementation of
> > method/behaviour they are testing, foo.exists function in my case.
> > [...]
>
> I'm re-reading my own post and realising, aren't 2) and 3)
> breaking the guideline [1] about writing tests to the interface,
> not the implementation?
>
> I think the original version of test_exists_ExistingItemId_ReturnTrue
> I've got is written to the interface and mocks only bare minimum
> of required dependencies.
>
> Anyone could help me out to get in order with
> terminology vs practice vs best practices here?
>
> How much unit tests should know about implementation of what is being
> tested?
>
> [1] "Stop Mocking, Start Testing" Augie Fackler and Nathaniel Manista at
> Google
> http://youtu.be/Xu5EhKVZdV8?t=18m9s
>
>
> Best regards,
> --
> Mateusz Łoskot, http://mateusz.loskot.net
>
>  
>