mercredi 16 août 2017

Stub function is not being called, instead the real version is called. Why?

I'm trying to test a function that calls two other functions that connect to amazon AWS. With this in mind I don't want to call the real functions that call AWS - I'm trying to stub them. However, every time I run my test it is calling the real functions not my stubs.

I'm kinda new to testing and might be missing something but could not find a solution on any other question. I'm using jasmine and sinon

My code is as follow:

const cache = new Map<string, Set<string>>();
//Function to be tested - inside class XYZ
export function functionToBeTest(events: Event[]): Promise<Event[]> {
    cache.clear();
    let promises: Array<Promise<void>> = [];
    promises = events.map((event) => {
        let foundId: string;
        return functionA(event.Id)
            .then((id: string) => {
                foundId = id;
                if (!cache.has(id)) {
                    return functionB(id);
                }
            })
            .then((regDev) => {
                cache.set(foundId, new Set(regDev));
            })
            .catch((err) => {
                log.error({ err: err });
                return Promise.reject(err);
            });
    });

    return Promise.all(promises)
        .then(() => {
            return Promise.resolve(events)
        });
}

The functions I want to stub:

export function functionA(id: string): Promise<string> {//Return Promise<string>
//some code
return anotherFunction(id);
}

export function functionB(organizationId: string): Promise<string[]> {
//Goes to amazon do something and return Promise<string[]>
}

I omitted the implementation of functionA and functionB since I don't care about what they do or how I just need to stub them so I test the logic inside functionToBeTest.

My test suite is as follow:

import * as sinon from 'sinon';

describe('testing functionToBeTest()', () => {

    let stubA: sinon.SinonStub;
    let stubB: sinon.SinonStub;

    beforeEach(() => {
        stubA = sinon.stub(xyz, 'functionA');
        stubA.onFirstCall().resolves('1');
        stubA.onSecondCall().resolves('2');
        stubB = sinon.stub(xyz, 'functionB');
        stubB.onFirstCall().resolves(['1']);
        stubB.onSecondCall().resolves(['2']);
    });

    afterEach(() => {
        stubA.restore();
        stubB.restore();
    });

    it('should populate the cache', (done) => {
        let events: Event[] = [
            {
                timestamp: 1,
                eventType: EventType.PC,
                id: '1'
            },
            {
                timestamp: 2,
                eventType: EventType.BU,
                id: '2'
            }
        ];
        xyz.functionToBeTest(events)// Here should call the real function and inside it should call the stub functions
        .then((result) => {
            //expectations
        })
        .then(() => {
            done();
        })
        .catch((err) => {
            done.fail(err);
        });
    });
});

As I said, when I run this test it never call the stub functions and always return me errors from inside the real functions (that should be stubbed).

Can anyone help with this? I might be doing the stub wrong but I can't see what is wrong.

Aucun commentaire:

Enregistrer un commentaire