Skip to content

Checking received calls seems to be off at times #45

@sluebbert

Description

@sluebbert

I seem to be running into an oddity when asserting that calls were made. The received method seems to be thrown off somehow based on the stripped down test shown below.

import { Substitute, Arg } from '@fluffy-spoon/substitute';

class Transaction
{
	public Id: number;
	public Description: string;

	constructor(id: number, description: string)
	{
		this.Id = id;
		this.Description = description;
	}
}

interface IDataAccess
{
	insertTransaction(transaction: Transaction): void;
	updateTransaction(transaction: Transaction): void;
}

class Manager
{
	private dataAccess: IDataAccess;

	constructor(dataAccess: IDataAccess)
	{
		this.dataAccess = dataAccess;
	}

	public run()
	{
		this.dataAccess.insertTransaction(new Transaction(1, 'milk'));
		this.dataAccess.updateTransaction(new Transaction(2, 'gas')); // Commenting out this line will cause the test to pass
	}
}

describe('Test', function()
{
	it('Tests', () =>
	{
		var dataAccess = Substitute.for<IDataAccess>();

		var manager = new Manager(dataAccess);
		manager.run();

		dataAccess.received().insertTransaction(Arg.is(x => x.Description == 'milk'));
	});
});

Running my test fails with the unexpected message:

$ ./node_modules/mocha/bin/mocha test.ts --require ts-node/register


  Test
    1) Tests


  0 passing (18ms)
  1 failing

  1) Test
       Tests:
     Error: Expected 1 or more calls to the method updateTransaction with arguments [Argument {
  description:
   "{predicate function (x) { return x.Description == 'milk'; }}",
  matchingFunction: [Function] }], but received none of such calls.
All calls received to method updateTransaction:
-> call with arguments [Transaction { Id: 2, Description: 'gas' }]
      at InitialState.assertCallCountMatchesExpectations (node_modules/@fluffy-spoon/substitute/src/states/InitialState.ts:46:15)
      at FunctionState.apply (node_modules/@fluffy-spoon/substitute/src/states/FunctionState.ts:48:30)
      at Context.apply (node_modules/@fluffy-spoon/substitute/src/Context.ts:45:28)
      at Object.apply (node_modules/@fluffy-spoon/substitute/src/Context.ts:19:29)
      at Context.<anonymous> (test.ts:48:25)
      at processImmediate (internal/timers.js:443:21)

The error message is complaining about the method updateTransaction, even though I only stated that I am interested in asserting that insertTransaction was called. It does show the correct context of what really happened at runtime though, just with the wrong method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions