Skip to content

There are legitimate cases to use td.verify #389

@Ccz-Chen

Description

@Ccz-Chen

Description

When you want a software to maintain consistent behaviour, you may want to use unit test to ensure code change won't break. There are some cases to use td.verify.

Issue

The warning
'test double was both stubbed and verified with arguments, which is redundant and probably unnecessary' is not necessary in some cases. Can this warning be turned off?

Examples

  1. Use unit test to ensure future code change will not cause regression bug

In the following example, having stubbed does not ensure runProcess will always perform all the 3 steps.
We have to put td.verify in to ensure these 3 steps should be performed all times.

let td = require('testdouble');
let mock: any;

it('process_should_contains_3_steps', () => {

td.when(mock.performStepA(td.machers.anything()))
    .thenResolve({ status: 'ok' })

td.when(mock.performStepB(td.machers.anything()))
    .thenResolve({ status: 'ok' })

td.when(mock.performStepC(td.machers.anything()))
    .thenResolve({ status: 'ok' })

myClass.runProcess()
    .then((result) => {
        td.verify(mock.performStepA(td.machers.anything()), { times: 1 });
        td.verify(mock.performStepB(td.machers.anything()), { times: 1 });
        td.verify(mock.performStepC(td.machers.anything()), { times: 1 });
    })

});

  1. Use unit tests to ensure it process correctly based on data

In this example, setup of stub is outside of individual unit test. The behaviour of unit tests will be different based on test data. We use td.verify to ensure code change won't break.

let td = require('testdouble');
let mock: any;

td.when(mock.perform(td.machers.anything()))
    .thenResolve({ status: 'ok' })

it('process_should_perform_on_data_1', () => {
    myClass.runProcess()
        .then((result) => {
            td.verify(mock.perform(td.machers.anything()), { times: 1 });
        })
});

it('process_should_not_perform_on_data_2', () => {
    myClass.runProcess()
        .then((result) => {
            td.verify(mock.perform(td.machers.anything()), { times: 0 });
        })
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions