tennisgent / quickmock

quickmock is an simple service for automatically injecting mocks into your AngularJS unit tests using Jasmine or Mocha
http://tennisgent.github.io/quickmock
34 stars 14 forks source link

Annotating with $inject property not supported #10

Open Mojova opened 9 years ago

Mojova commented 9 years ago

You can annotate dependencies in angular with the $inject property:

angular
    .module('foo')
    .factory('FooFactory', FooFactory);

FooFactory.$inject = ['dep1', 'dep2', 'dep3'];

function FooFactory(dep1, dep2, dep3) {
    //code
}

This isn't compatible with 1.0.7 of quickmock. Running the library through a debugger reveals that the problem is at line 35:

for(var i=0; i<currProviderDeps.length - 1; i++){
    var depName = currProviderDeps[i];
    mocks[depName] = getMockForProvider(depName, currProviderDeps, i);
}

depName is undefined for all i.

Changing the loop in question into

for(var i=0; i<currProviderDeps.$inject.length; i++){
    var depName = currProviderDeps.$inject[i];
    mocks[depName] = getMockForProvider(depName, currProviderDeps, i);
}

works for the first iteration. For subsequent tests in the suite, it seems to revert to "normal" behavior, i.e. no $inject.

tennisgent commented 9 years ago

You're right. I need to fix support for $inject. I had it in there once but apparently broke it in an update somewhere along the line. Sorry. I'll get it fixed.

mitchogaard commented 8 years ago

Just submitted a pull request to fix this issue, @tennisgent. Thanks for this library, btw.

mitchogaard commented 8 years ago

@Aakkosti Looks like @tennisgent merged my pull request in. Do you want to grab the latest (I believe it's untagged right now), verify that it's fixed, and close this?