amoss icon indicating copy to clipboard operation
amoss copied to clipboard

Mixing of behaviours of `when` and `expects` could be better when applied to a single method

Open bobalicious opened this issue 4 years ago • 1 comments

When applying an expects that is more specific than a when for a single method, it seems reasonable to expect the expects definition to be matched before the when is. However, the when is always matched first.

This means it's not possible to apply a generic 'catch all' when to a mock.

For example, given the following

controller
    .expects( 'scheduleDelivery' )
        .withParameterNamed('deliveryPostcode').setTo( deliveryPostcode )
        .andParameterNamed( 'deliveryDate' ).setTo( deliveryDate )
        .returning( true )
    .also()
    .when( 'scheduleDelivery' ).returns( false );

Reasonable to expect that a call to scheduleDelivery with the specified parameters to match the expects, and all other (and subsequent) calls to match the when.

Similarly:

controller
    .expects( 'scheduleDelivery' ).returning( true )
    .also()
    .when( 'scheduleDelivery' ).returns( false );

Reasonable to expect the first call to scheduleDelivery would match the expects, and then the remainder to match the when.

This is not the case, and the when is always matched first.

In both these examples, controller.verify will always fail, since it's impossible to empty the expects stack.

bobalicious avatar Jan 22 '21 06:01 bobalicious

Not really possible to define how 'specific' a definition is. Priority could be the first one that's defined to match, or the expects should match before the whens. To be decided.

bobalicious avatar May 08 '21 07:05 bobalicious