bobalicious / amoss

Amoss - Apex Mock Objects, Spies and Stubs - A Simple Mocking framework for Apex (Salesforce)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enhanced 'setTo' capabilities for Parameters

bobalicious opened this issue · comments

Review this with reference to #60 - not all of this will be valid any more

In order to make it easier to more fuzzily check parameter values and the properties of sObjects that are passed into test doubles, the parameter matching capabilities of the parameter syntax should be improved.

This removes common reasons for falling back to 'handledBy' when actual processing is not required - particularly in expects and allows, simplifying tests.

Potentially, this ticket will be split into multiple ones as different capabilities are added.

Implement, amongst others:

  • set() - checks the value is not null.
  • containing( value ) - checks the parameter is a String and contains the given value.
  • property( propertyName ).setTo( value ) - checks the parameter is an sObject, and has the stated property set to the stated value.
  • listElement( index ).setTo( value ) - checks that the parameter is a List, and has the object at the stated index set to the stated value.
  • withElementsSetTo( collectionValue ) - checks that the parameter is a collection of the same type, and consists solely of the object instances passed, allowing the creation of (for example) a new list based on an existing one.

In every instance, where setTo is available, assume that the following are also available:

  • set
  • containing
  • property
  • listElement
  • allListElements
  • mapElement
  • allMapElements

It should be possible to specify several configurations for:

  • property
  • listElement
  • allListElements
  • mapElement
  • allMapElements

Though it does not make sense to do the same for:

  • setTo
  • set

It's open to debate whether the following should allow repetitions:

  • contains

For example, the following should be possible:

classToDoubleController
    .expects()
        .method( 'methodThatShouldNotGetCalledWithPersonAccount' )
        .withParameterNamed( 'contact' )
            .allListElements()
                .property( 'IsPersonAccount' ).setTo( false )
            .listElement( 1 )
                .property( 'LastName' ).contains( 'Test' );

Potentially, could be something like:

classToDoubleController
    .expects()
        .method( 'methodThatShouldNotGetCalledWithPersonAccount' )
        .withParameterNamed( 'contact' )
            .beingAList()
                .withAllElements()
                    .havingProperty( 'IsPersonAccount' ).setTo( false )
                .withElement( 1 )
                    .havingProperty( 'LastName' ).containing( 'Test' );

Positional parameter notation should be enhanced to use the same matching, by allowing the additional syntax:

  • parameter().setTo()
  • parameter().property( 'PersonAccount' ).setTo( false )

A little dubious that the map implementation will actually work though, due to the difficulties in casting maps.

Superseded by other tickets