Skip to content

Commit 3bf4390

Browse files
wesalvaroIgorMinar
authored andcommitted
fix(mocks): always call functions injected with inject with this set to the current spec
Currently when a function is injected inside of a test we set the context to undefined which is a bug. Closes angular#6102
1 parent e7ac7aa commit 3bf4390

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/ngMock/angular-mocks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2125,7 +2125,7 @@ if(window.jasmine || window.mocha) {
21252125
window.inject = angular.mock.inject = function() {
21262126
var blockFns = Array.prototype.slice.call(arguments, 0);
21272127
var errorForStack = new Error('Declaration Location');
2128-
return isSpecRunning() ? workFn() : workFn;
2128+
return isSpecRunning() ? workFn.call(currentSpec) : workFn;
21292129
/////////////////////
21302130
function workFn() {
21312131
var modules = currentSpec.$modules || [];

test/ngMock/angular-mocksSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,25 @@ describe('ngMock', function() {
864864
});
865865

866866

867+
describe('this', function() {
868+
869+
it('should set `this` to be the jasmine context', inject(function() {
870+
expect(this instanceof jasmine.Spec).toBe(true);
871+
}));
872+
873+
it('should set `this` to be the jasmine context when inlined in a test', function() {
874+
var tested = false;
875+
876+
inject(function() {
877+
expect(this instanceof jasmine.Spec).toBe(true);
878+
tested = true;
879+
});
880+
881+
expect(tested).toBe(true);
882+
});
883+
});
884+
885+
867886
// We don't run the following tests on IE8.
868887
// IE8 throws "Object does not support this property or method." error,
869888
// when thrown from a function defined on window (which `inject` is).

0 commit comments

Comments
 (0)