How to verify that a method was NOT called?

4,167 views
Skip to first unread message

Kristopher Schultz

unread,
Oct 22, 2012, 8:45:30 PM10/22/12
to mock...@googlegroups.com
What is the most appropriate way to write a test that verifies that a particular method was NOT called? For example, I want to implement a test described as "When Widget.methodA() is called it should not trigger Widget.methodB()".

Hob Spillane

unread,
Oct 23, 2012, 2:09:21 PM10/23/12
to mock...@googlegroups.com

how about:

expect(mock.methodB()).times(0)

-Hob

Kristopher Schultz

unread,
Oct 24, 2012, 6:38:13 PM10/24/12
to mock...@googlegroups.com
Thanks, Hob. That works. So does...

expect(mock.methodB()).never()

The "teach a man to fish" answer turned out to be referencing the API docs for mockolate.ingredients.MockingCouverture which defines all sorts of useful constraint methods.

On Tuesday, October 23, 2012 2:10:49 PM UTC-4, Hob wrote:

how about:

expect(mock.methodB()).times(0)

-Hob


Drew Bourne

unread,
Oct 25, 2012, 12:24:49 PM10/25/12
to mock...@googlegroups.com
An alternative (if you build from master) is to use a Test Spy like so: 

    var spy:Spy = spy(mock.methodB());
    // do rest of test
    assertFalse(spy.called());

Using a test spy in this case may be overkill, but if you need more granular access to the invocations it can be useful. 

See:

cheers, 
Drew

William Madden

unread,
Oct 25, 2012, 10:09:52 PM10/25/12
to mock...@googlegroups.com
Can't you do expect(a.b()).never()?
Reply all
Reply to author
Forward
0 new messages