Reputation: 14736
I'm building a cordova / phonegap app along with AngularJS. In my app, I'm using some native plugins from cordova, like the geolocation plugin.
During my tests, this plugins aren't available (because phonegap isn't present), $window.plugins
returns undefined
in my tests. And of course, $window.plugins.anotherPlugin
also fails.
Therefore, I have to mock out this parts for my tests. Currently, I'm doing it this way
beforeEach(function() {
$window.plugins = {
anotherPlugin: {
foo: function() {}
}
};
});
But what if in the future, another library uses the $window.plugins
namespace, and I'm overwriting it in my tests? This would break other tests. With the way above, I would have to do some cleanup after my tests, to ensure that the old values of $window.plugins
are set back. I think, this way isn't very clean, and I wonder if there is a better way to do this with jasmine.
So my question is: How can I spy on $window.plugins.anotherPlugin
by creating dummy functions with jasmine, without affecting other tests, when $window.plugins
doesn't exist?
Upvotes: 1
Views: 3046
Reputation: 88
You should load the module of the part you want to test and generate a mock module, which provides the spies.
beforeEach(function () {
module('ModuleUnderTest', function($provide) {
var windowMock = {
plugins: {
anotherPlugin: jasmine.createSpyObj('anotherPlugin', ['foo', 'bar'])
}
};
// substitute all dependencies with mocks
$provide.value('$window', windowMock);
});
});
Beware, I have not tested the code, but you should get the gist.
Upvotes: 1