How To Unit Test With Jasmine On Multiple Chained Functions With Returns?
Solution 1:
I don't if this solution could help you or not
When we write test case, especially test for functions, we tend to test the input and output value for the function.
And for me, I think we shouldn't touch the primitive function of Array, Object
by calling jasmine mock.
If you want to know if your function is working properly in this case.
For example:
describe('$scope.filterData', function() {
//paramsvar data = {
key1: "value1",
key2: "value2",
key3: "value3"
}
var allowedKeys = ["key1", "key2"];
it('should return 2 key', function() {
var expected = {
key1: "value1",
key2: "value2",
}
var value = $scope.filterData(data, allowedKeys);
expect(JSON.stringify(value)).toEqual(JSON.stringify());
});
});
Of course we have to mock some functions sometimes, like when we have the http request and have to wait, or we have function from other places to use and want to mock them.
But in this case, your function is not really necessary for expect
some function to be called
, it's simply enough and don't depend on anything else.So best we should only focus on the input and output values of function
Solution 2:
First of all, Object
does not have a function filter
and your jasmine.createSpyObj
is literally useless. As @Luan Phan says in his answer, we usually tend to test the input and output value for the function. Javascript built-in functions do not need to be tested in our tests.
But, if you would like to know, for example, if Object.keys
was called inside your function, here is an example
it('should func', function () {
spyOn(Object, 'keys').and.callThrough();
$scope.filterData(data, allowedKeys);
expect(Object.keys).toHaveBeenCalled();
});
The same can be done for the rest of the built-in functions used in filterData
it('should func', function () {
spyOn(Object, 'keys').and.callThrough();
spyOn(Array.prototype, 'filter').and.callThrough();
spyOn(Array.prototype, 'reduce').and.callThrough();
$scope.filterData(data, allowedKeys);
expect(Object.keys).toHaveBeenCalled();
expect(Array.prototype.filter).toHaveBeenCalled();
expect(Array.prototype.reduce).toHaveBeenCalled();
});
If you really need to mock what one of the built-in functions returns, here is an example
it('should func', function () {
const mockResult = {};
spyOn(Array.prototype, 'reduce').and.returnValue(mockResult);
const result = filterData(data, allowedKeys);
expect(result).toBe(mockResult);
});
Again, Javascript's built-in functions already have tests written in some other place, we don't need to test it in our test, our focus should be on the functions we write.
Hope it helps
Post a Comment for "How To Unit Test With Jasmine On Multiple Chained Functions With Returns?"