Is it posible to mock object for service? Is it posible to mock value of cfg.user.core.token? Im mocking httpBackend like this:
$httpBackend.when("GET","/check/").respond({
data: ['success']
});
This is my code:
.service("session", function ($window, $location, $http, cfg) {
var service = {};
service.check = function () {
$http.get("/chceck/" + cfg.user.core.token).then(function (response) {
if (response.data && response.data.success !== true) {
service.logout();
}
});
};
return service;
})
my part of test:
it("check ", function () {
spyOn(session, 'check').and.callThrough();
session.check();
expect(session.check).toHaveBeenCalled();
$httpBackend.flush();
});
Assuming cfg is an angular constant:
angular.module('mymodule')
.constant('cfg', {
user: {
core: {
token: 'token'
}
}
});
So in the Jasmine tests you can stub this constant:
describe('mytest', function () {
var
cfgStub = {
user: {
core: {
token: 'stubToken'
}
}
};
beforeEach(module('mymodule'));
beforeEach(module(function($provide) {
$provide.constant('cfg', cfgStub);
}));
...
});
And use cfgStub wherever you want in the tests.
Related
I am writing unit test for $interval. The code is like this:
angular.module('serviceInvoker', []).
run(function($rootScope, $http, $interval, DateTimeFormatter) {
$rootScope.loadData = function(serviceName, dataName) {
$http.get(serviceName)
.then(function(result) {
serviceSuccessFunc(result, dataName)
})
.catch(function(error) {
serviceErrorFunc(error, dataName)
});
$rootScope.current = moment().toISOString();
$rootScope.now = moment($rootScope.current).format(DateTimeFormatter.DayHoursFormat);
};
$rootScope.autoRefresh = function(serviceName, dataName, interval) {
$interval(function() {
$rootScope.loadData(serviceName, dataName)
}, interval);
};
var serviceSuccessFunc = function(result, dataName) {
$rootScope[dataName] = result.data;
};
var serviceErrorFunc = function(error, dataName) {
$rootScope[dataName] = error;
};
});
The test code is like this:
describe('serviceInvoker', function() {
beforeEach(module('serviceInvoker'));
beforeEach(module(function($provide) {
$provide.value('DateTimeFormatter', {
DayHoursFormat: 'HH:mm'
});
$provide.value('serviceSuccessFunc', jasmine.createSpy());
$provide.value('serviceErrorFunc', jasmine.createSpy());
}));
var $interval;
beforeEach(inject(function (_$rootScope_, _$interval_, _serviceSuccessFunc_, _serviceErrorFunc_) {
$rootScope = _$rootScope_;
scope = $rootScope.$new();
$interval = _$interval_;
serviceSuccessFunc = _serviceSuccessFunc_;
serviceErrorFunc = _serviceErrorFunc_;
}));
describe("loadData function ", function () {
it("should expose loadData function to $rootScope", function () {
expect(angular.isFunction($rootScope.loadData)).toBe(true);
});
it("should be called", inject(function($http) {
spyOn($rootScope, 'loadData');
$rootScope.loadData('service', 'data');
expect($rootScope.loadData).toHaveBeenCalledWith('service', 'data');
}));
});
describe("autoRefresh function ", function () {
it("should expose autoRefresh function to $rootScope", function () {
expect(angular.isFunction($rootScope.autoRefresh)).toBe(true);
});
it("should be called", function() {
var $intervalspy = jasmine.createSpy('$interval', $interval);
spyOn($rootScope, 'autoRefresh').and.callThrough();
$rootScope.autoRefresh('service', 'data','interval');
expect($rootScope.autoRefresh).toHaveBeenCalledWith('service', 'data', 'interval');
expect($intervalspy).toHaveBeenCalled();
// expect($intervalspy).toHaveBeenCalledWith(jasmine.any(Function), 1000);
});
});
});
But there is an error for interval: Error: Expected spy $interval to have been called.
I don't know how to write unit test for interval which is inside a function(in a run block), can anyone give me some help?
$intervalspy isn't used anywhere but the test itself. It isn't called.
As any other tested service, $interval should be spied, mocked or stubbed. It can be spied with a decorator like that, and stubbing is even simpler:
beforeEach(() => {
module({ $interval: jasmine.createSpy() })
});
...
$rootScope.autoRefresh('service', 'data', 'interval');
expect($interval).toHaveBeenCalledWith(jasmine.any(Function), 'interval');
expect($rootScope.loadData).not.toHaveBeenCalled(),
var intervalCallback = $interval.calls.first().args[0];
intervalCallback();
expect($rootScope.loadData).toHaveBeenCalledWith(...),
I am having a controller and service like below
(function () {
var mockController = function ($scope, MockService) {
$scope.message = "This is a text message";
$scope.getCities = function () {
$scope.places = [];
MockService.getCities().then(function (response) {
var places = response.data["weather-app:root"].city;
if (places) {
if (Array.isArray(places)) {
$scope.places = places;
} else {
$scope.places.push(places);
}
}
});
};
};
var mockService = function ($http) {
this.getCities = function () {
return $http.get("../rest/url", {
headers: {
'Accept': 'application/yang.data+json'
}
});
};
};
angular.module("MockApp", [])
.service("MockService", mockService)
.controller("MockController", mockController);
}())
I created a mock service like below for mocking the service for unit testing.
(function () {
angular.module('mock.service', [])
.service('MockService', function ($q) {
var mockService = {};
mockService.getCities = function () {
var mydata = {
"weather-app:root": {
"city": [
{
"city-name": "Chennai"
, "country-name": "India"
}
, {
"city-name": "Mangalore"
, "country-name": "India"
}
]
}
}
return $q.when(mydata);
};
return mockService;
});
}());
My test case is like
describe("MockController", function () {
var $scope;
beforeEach(function () {
module("MockApp");
beforeEach(module('mock.service'));
inject(function (_$controller_, _$rootScope_, _MockService_) {
$scope = _$rootScope_.$new();
controller = _$controller_("MockController", {
$scope: $scope
, MockService: _MockService_
});
});
});
describe("Test", function () {
it("Should be Bangalore", function () {
$scope.getCities();
console.log($scope.places);
});
});
});
the problem is that the then method in controller is not getting called. How can I resolve the issue ?
Three things to fix...
Don't nest the beforeEach calls. You can init multiple modules with module.
beforeEach(function() {
module('MockApp', 'mock.service');
// and so on
Your mock data does not quite match what you'd see from an $http based promise response
return $q.when({data: mydata});
In order to process promises, you need to trigger a digest cycle
it("Should be Bangalore", function() {
$scope.getCities();
$scope.$apply();
console.log($scope.places);
});
I want test my angular app with mocha,sinon and chai.
Especially I interest in submit function. How to create mock or stub for LoginResoure to test this function.
Thanks!
(function () {
'use strict';
class LoginController {
constructor($state,LoginResource) {
this.resource = LoginResource;
this.$state = $state;
this.credentials = {};
}
submit() {
let promise = this.resource.login(this.credentials);
promise.then(()=>{
changeState()
}
}
changeState() {
this.$state.go('home');
}
}
angular.module('app.login').controller('LoginController', LoginController);
})();
(function () {
'use strict';
class LoginResource {
constructor($resource, API_LOGIN) {
this.$resource = $resource(API_LOGIN,{'#id':id})
}
login(data) {
return this.$resource.save(data).$promise;
}
}
angular.module('app.login').service('LoginResource', LoginResource);
})();
EDIT:
Previously I do it with jasmine in next way:
let deferred = $q.defer();
deferred.resolve('Remote call result');
mockPeopleResource = {
createPerson: jasmine.createSpy('createPerson').and.returnValue(deferred.promise)
};
or if I want mock #resource
mockThen = jasmine.createSpy();
mockGetPeoplePromise = {then: mockThen};
mockUpdate = jasmine.createSpy().and.returnValue({$promise: mockPromise});
mockSave = jasmine.createSpy().and.returnValue({$promise: mockPromise});
mockGetPeopleQuery = jasmine.createSpy().and.returnValue({$promise: mockGetPeoplePromise});
mockResource = jasmine.createSpy().and.returnValue({
get: mockGet,
update: mockUpdate,
save: mockSave,
query: mockGetPeopleQuery
});
If you want to mock a service, you can create a test module when you set the mocked value:
beforeEach(function() {
angular.module('test', []).factory('LoginResource', function($q) {
return {
/* You can mock an easy login function that succeed when
data >= 0 and fails when data < 0 */
login: function(data) {
return $q(function(resolve, reject) {
if (data >= 0) return resolve();
reject();
});
}
};
});
module('app.login', 'test');
});
In my Controller I've defined the following service:
CrudService.getAllGroups().$promise.then(
function (response) { $scope.groups = response; },
function (error) { //error code.. }
);
Well, I want to test this service whether it gets a response or not. In test script at first I've defined a function to check whether the service is defined at all.
Test code:
describe('Ctrl: TestCtrl', function () {
beforeEach(module('testApp'));
var scope,
CrudService,
ctrl,
backend;
beforeEach(inject(function ($controller, $rootScope, _CrudService_, $httpBackend) {
scope = $rootScope.$new();
ctrl = $controller('TestCtrl', {
$scope: scope
});
CrudService = _CrudService_;
backend = $httpBackend;
}));
it('should defined the service getGroups', function () {
expect(CrudService.getGroups).toBeDefined();
});
//this is wrong!
it('should returns a successful response', function () {
backend.expectGET('http://localhost:63831/api/group').respond(200, 'success');
backend.flush();
});
});
I don't know how to get a response in the test. I'm new in unit testing and need some help.
For a better comprehension here is the service code:
//CrudService file:
...
return {
getAllGroups: function () {
return ResService.group.query();
}
}
...
//ResService file:
return {
group: $resource(baseUrl + '/api/group/:Id', {
Id: '#Id'
}, {})
}
Do anyone has an idea?
It's incorrect in the sense that it's not a unit test. If you are testing controller here, then you should mock CrudService and test that $scope.groups has been assigned correctly.
beforeEach(function () {
module(function ($provide) {
$provide.factory('CrudService', function () {
return {
getAllGroups: function () {
return {
$promise: null // return an actual promise here
}
}
}
});
});
});
it('should set groups', function () {
expect($scope.groups).toEqual('success')
});
And you need a separate spec to test if CrudService calling backend correctly.
We're unit testing our services and facing issue spying on methods with arguments of dependent services.
I am writing unit tests for ServiceA
ServiceA.js
angular.module("App").service("ServiceA", function($http, ServiceB) {
this.detail = null;
this.method = function(id){
var sevrB = new ServiceB();
return sevrB.getId(1).then(function(response) {
this.detail = response.data;
});
};
});
ServiceB.js (is a factory)
(function () {
var dependencies = [
'../module'
];
define(dependencies, function (module) {
return module.factory('ServiceB', function ($http) {
var ServiceB= function () {
this.id = null;
};
ServiceB.prototype.getId = function(Id) {
return $http.get('/test/');
}
}
}());
Unit test code
describe('Testing ServiceA', function () {
var serviceA, serviceBMock;
beforeEach(function () {
var _serviceBMock = function () {
return {
getId:function(id){
return 'test';
}
};
};
angular.module('ServiceAMocks', [])
.value('ServiceB', _serviceBMock);
});
beforeEach(module('ServiceAMocks'));
beforeEach(inject(function (_ServiceA_, _ServiceB_) {
serviceA=_ServiceA_;
serviceBMock=_ServiceB_;
});
it('retrive Id', function () {
spyOn(serviceBMock,'getId').and.Return('test');
serviceA.method(1);
});
});
I am spying on getId method of ServiceB from ServiceA and if i mocked ServiceB as function i am getting error below
Error: getId() method does not exist
at jasmineInterface.spyOn
If I mock serviceB as object then i get error as
TypeError: object is not a function
var _serviceBMock = {
getId:function(id){
return 'test';
}
}
And I am not sure of testing promise in this scenario.
This version supports Jasmine 1.3
I’m injecting $q service as ServiceB wants to call method then. We can even go forward and resolve returned promise, but this is next step in testing.
Answer to previous version of question, where AngularJS injects instance of serviceB
describe('ServiceA', function () {
var serviceA, ServiceB, $q;
beforeEach(function () {
module('App');
});
beforeEach(function () {
module(function ($provide) {
$provide.value('ServiceB', {
getId: jasmine.createSpy('ServiceB.getId').andCallFake(function () {
return $q.all();
})
});
});
});
beforeEach(inject(function (_ServiceA_, _ServiceB_, _$q_) {
$q = _$q_;
serviceA = _ServiceA_;
ServiceB = _ServiceB_;
}));
describe('.method()', function () {
it('returns ServiceB.getId() argument', function () {
serviceA.method(1);
expect(ServiceB.getId).toHaveBeenCalledWith(1);
});
});
});
jsfiddle: http://jsfiddle.net/krzysztof_safjanowski/sDh35/