Hi I am developing web application in Angular JS. I am writing unit test cases using Jasmine framework. I am trying to mock some services. I am not able to call service with multiple parameters. Below is my unit test code.
it('update is allowed false', async(() => {
let service = fixture.debugElement.injector.get(UserOnboardEndpointMock);
spyOn(service, 'updateUser').and.callThrough();
fixture.whenStable().then(() => {
expect(service.updateUser).toHaveBeenCalledWith(jasmine.any(true,"101"));
})
}));
Below is my service.
updateUser<T>(event: boolean, userid: string): Observable<T>{
var updateUserResult = { result: true } as any;
return Observable.of<T>(updateUserResult);
}
I have tried as below to call service but nothing worked out.
expect(service.updateUser).toHaveBeenCalledWith(true,"101");
expect(service.updateUser).toHaveBeenCalledWith([true]["101"]);
Can someone help me to call my mock services? Any help would be appreciated. Thank you
Since jest 23.0 there is .toHaveBeenNthCalledWith(nthCall, arg1, arg2, ....) https://facebook.github.io/jest/docs/en/expect.html#tohavebeennthcalledwithnthcall-arg1-arg2-
So you could make an assertion like:
expect(service.updateUser).toHaveBeenNthCalledWith(1, true,"101");
Related
I'm testing an AngularJS service with Jasmine. The service calls a function in another service using an enum from another module as a parameter.
public getSavedColumns = (): ng.IPromise<GridColumn[]> => {
return this.productSettingsService.readProjectSetting(
psfc.ApplicationId.Calculator, this.getColumnStorageProperty())
.then(response => { /**/ });
};
psfc.ApplicationId is an enum in another module:
export enum ApplicationId {
Calculator = 2636
}
The Jasmine tests fail with the error:
TypeError 'undefined' is not an object (evaluating 'psfc.ApplicationId.Calculator')
I thought I could mock the enum in the test, but it doesn't change the outcome.
beforeEach(() => {
angular.mock.module('pw_psfc',
$provide => {
$provide.constant('psfc.ApplicationId.Calculator', 0);
});
});
I was able to solve my problem by including the file with the exported enum in the karma configuration. Five months is a pretty slow learning curve, but I'm getting there! =~)
I am writing unit test case for AngularJs Post operation. I have created mock service. I am trying to write test case as below.
Below are the input fields to pass as model.
userid,tenantid,username,emailaddress.
Below is the service I created.
addUser<T>(): Observable<T> {
var addUserResult = { result: true } as any;
return Observable.of<T>(addUserResult);
}
Below is the test case I am trying to invoke post operation.
it('should add the users', fakeAsync(() => {
}));
Can someone help me to write the above test case? Any help would be appreciated. Thank you.
I am working on a code that has some unit test failing which I am trying to run. The unit test is
it('opens a confirmation dialog when deleting a layer and calls the delete wizard', function () {
var numLayers = scope.service.layers.length;
scope.deleteLayer({
serviceName: 'service1',
layerName: 'testLayer'
});
//Make sure confirmation dialog has been called
expect(bootbox.confirm).toHaveBeenCalled();
//the layer is NOT removed from the list
expect(scope.service.layers.length).toBe(numLayers);
});
I keep getting the error:
Unexpected request Get /api/configuration/utilities/userpreferences/f=json
I am trying to create a spyon for taking care of this api call. I am using this
spyOn(resourceFactory.configuration.utilities.userpreferences, 'get').and.callFake(function () { });
I have also defined this in the describe scope and injected in beforeeach
var resourceFactory = {
configuration: {
utilities: {
userpreferences: {
get: function () { }
}
}
}
};
The thing is I dont care about this call to API since I am not testing it.
When I use $httpBackend and do the following
/$httpBackend.whenGET("/api/configuration/utilities/userpreferences?f=json").respond({});
it works but I am not sure if this is the right way to do it. Especially since its not used anywhere else in the project.
Can you help with this.
Whilst attempting to run integration tests with Angular 2 and Karma test runner the following issue became clear. A test was always passing even when it should have been failing.
The issue occurs when the expect() method is placed inside the subscribe() method of an Observable.
The need to do this arose as the test would subscribe to the Observable and then continue processing the rest of the test before the Observable has finished executing.
However, placing the expect within the subscribe() method automatically causes the test to pass even when there are very obvious syntax errors:
it('should pass or fail', inject([Service], (_service : Service) => {
let result = _service.returnObservable();
result.subscribe((sfasdasdaa23231gr) => {
expect(r.isAfhahzdzd vailable).not.35q6w623tyrg /.0824568sfn toBe(truDDIDIDIDDIe);
});
}));
the previous code passes, but how? there are syntax errors everywhere. Does anyone know where this issue lies? In the testing or in the subscribe() method?
Because it's asynchronous processing, you should add the async method:
it('should pass or fail', async(inject([Service], (_service : Service) => {
let result = _service.returnObservable();
result.subscribe((sfasdasdaa23231gr) => {
expect(r.isAfhahzdzd vailable).not.35q6w623tyrg /.0824568sfn toBe(truDDIDIDIDDIe);
});
})));
This seems so, so easy, but I can't figure out why this simple code doesn't work.
I am adding a mock module to mock my API backend in my Angular E2E tests. I'm using Protractor 1.6.0. I need to pass additional arguments to the mocked module, which, according to the Protractor docs, is possible by just sending them as additional arguments. However, my function claims it has no arguments...
var mock = function() {
// This is undefined and arguments.length is 0....why???
var env = arguments[0];
var mocks = angular.module('mocks.login', ['MyApp', 'ngMockE2E']);
mocks.run(function($httpBackend) {
$httpBackend.whenGET(env.apiBase + '/companies').respond([]);
});
};
browser.addMockModule('mocks.login', mock, {apiBase: ""});
If it matters, I'm doing this in my Protractor config file in an onPrepare, as I'm trying to mock the API calls used during a user's login. Any help would be much appreciated.
Appearantely the args should be passed as array.
This is how it worked for me:
var test = {
val1: 'val1',
val2: 'val2'
};
browser.addMockModule('mockModule', function( args) {
angular.module('mockModule', [])
.value('TEST', args[0]);
}, [test]);
This works for me:
var mock = function(args) {
// args.apiBase should be available here
var env = args;
var mocks = angular.module('mocks.login', ['MyApp', 'ngMockE2E']);
mocks.run(function($httpBackend) {
$httpBackend.whenGET(env.apiBase + '/companies').respond([]);
});
};
browser.addMockModule('mocks.login', mock, {apiBase: ""});