Reputation: 1420
I have the following line of code inside a function I want to unit test:
var myStore = Ext.data.StoreManager.lookup("MyStore");
I am trying to mock the store inside my unit test as follows:
spyOn(Ext, 'data').and.returnValue({
StoreManager: {
lookup: function() {
return myMockStoreObjectForTestingPurposes;
}
}
});
However, for some reason the unit test is trying to use the real objects returned by Ext.data instead of my mock object.
Anyone know why this is happening? Should I be mocking Ext.data.StoreManager in a different way?
Upvotes: 2
Views: 258
Reputation: 820
Jasmine is expecting 'data' to be a function and it's actually an object.
I can see three solutions here, the easiest would be to spy on Ext.data.StoreManager, something like:
spyOn(Ext.data.StoreManager, 'lookup').and.callFake(function() {
return myMockStoreObjectForTestingPurposes;
});
This will actually replace every lookup call with your "fake" function.
The second would be replace the Ext object fully in your test, like:
beforeEach(
module('your-module-name', function ($provide) {
var mockedExt = {
data: {
StoreManager: {
lookup: function() {
return myMockStoreObjectForTestingPurposes;
}
}
}
};
$provide.value('Ext', mockedExt);
})
);
This is replace every inject of Ext
with mockedExt.
The last solution is almost the same as the one above but you pass that straight to your controller (assuming you're testing a controller), like:
beforeEach(
inject(function (_$controller_) {
var mockedExt = {
data: {
StoreManager: {
lookup: function() {
return myMockStoreObjectForTestingPurposes;
}
}
}
};
var myController = _$controller_('MyControllerName', {
Ext: mockedExt
});
})
);
Hope that helps :)
Upvotes: 1
Reputation: 966
I believe you should try with and.callFake instead of and.returnValue, it should return your mock object then. If i'm right, you can read more on this here:
http://jasmine.github.io/2.0/introduction.html
Best of luck.
Upvotes: 0