wardbell / bardjs

Spec helpers for testing angular v.1.x apps with Mocha, Jasmine and QUnit
MIT License
178 stars 34 forks source link

A mockService call using a replacement function doesn't result in a spy #18

Closed Barryrowe closed 8 years ago

Barryrowe commented 8 years ago

If mockService is called with a replacement function in a scenario like below:

var isFirstCall = true;
function myReplacementFunc(){
    if(isFirstCall){
        isFirstCall = false;
        return {test:"value"};
    }else{
        return undefined;
    }
}
mockService(MyService, {
    get: myReplacementFunc
}

the get function on the mocked service is not spied on properly, and so something like this (using jasmine-sinon):

expect(MyService.get).toHaveBeenCalledOnce();

fails with an error like:

Expected spy "function myReplacementFunc() {
        if (isFirstCall ) {
            isFirstCall = false;
            return { test: "value" };
        } else {
            return undefined;
        }
    }" to have been called twice. "function myReplacementFunc() {
        if (isFirstCall ) {
            isFirstCall = false;
            return { test: "value" };
        } else {
            return undefined;
        }
    }" was called 0 times.

I would have expected this to work the same as if I had just manually wired up the sinon spies like:

sinon.stub(MyService, 'get', myReplacementFunc);

It appears that if line 564 of the current distribution were changed from:

service[key] = value;

to

sinon.stub(service, key, value);

this would work as expected.

Barryrowe commented 8 years ago

This was fixed with the merge above.