Reputation: 73
I have a a factory which uses Angular resource to make API call. I then created a function called getObjectById and use the factory to query for that object and then modify the object before returning.
Service:
return getObjectById: function(id) {
return objectFactory.getById({ id: id }).$promise.then(function(response) {
return modifyObject(response.object);
});
}
I want to test that modifyObject() is working correctly but modifyObject is a private function.
So to test it i'm trying to mock object the response object factory is returning with a spy.
Test:
beforeEach(function() {
inject(function(objectFactory, $q) {
spyOn(objectFactory, 'getById').and.returnValue({ $promise: $q.when(readJSON('test/resources/object.json'))});
});
});
But everytime I run the test I get the error:
TypeError: Cannot read property 'returnValue' of undefined
If I can get any help on getting it to work that will be great. Even suggestion if i'm doing the layout wrong. Using latest Version Of Jasmine
Upvotes: 3
Views: 910
Reputation: 3038
It's a very late answer, but I just ran into the same issue in an AngularJS / Angular hybrid app. We are using jasmine v1, but imported jasmine v2 typing. Which means the TypeScript would not compile with v1 syntax but would fail with TypeError: Cannot read property 'returnValue' of undefined
.
The jasmine version needs to match the typing:
"karma-jasmine": "0.1"
means jasmine v1 and requires "@types/jasmine": "^1.3.0"
"karma-jasmine": "0.3"
would be jasmine v2 and requires "@types/jasmine": "2.8"
Upvotes: 1
Reputation: 594
You're missing the closing bracket
spyOn(objectFactory, 'getById').and.returnValue({ $promise: $q.when(readJSON('test/resources/object.json'))});
Upvotes: 0