Unit testing angular.js service (node.js server) in controller - angularjs

I'm new to the unit testing in the client side. My application uses the express.js, angularjs-ui-router and node.js. Currently i start writing the unit test cases for the application. I'm using Karma, Mocha, Chai, Sinon for unit testing.
My router config look like below:
$stateProvider
.state('drive', {
url: '/drive',
templateUrl: 'drive.jade',
controller: 'driveCtrl',
});
Controller:
angular.module('mApp').controller('driveCtrl', ['$scope', 'driveService',
function($scope, driveService) {
var driveInfo = driveService.get({}, function() {});
driveInfo.$promise.then(function(rs) {
var drivers = [];
//Logical operation
$scope.drivers = drivers;
});
}]);
Factory Resource:
mApp.factory('driveService', ['$resource', function($resource) {
return $resource('/drive/id/:id/', {id:'#id'});
}]);
The driveService is a factory which insides uses a angular.js $resources. I tried variety of options but nothings seems to be working (using the $httpbackend, $q). Can you help me out to write the way to test the controller by mocking the driveService.
Here's my unit test code:
var expect = require('chai').expect;
var sinon = require('sinon');
describe('Drive Service initialisation', function() {
var scope, controller, state, $q, mockDriveService, driveServiceResponse = [{name:'james', type: 'heavy'}], queryDeferred;
beforeEach(angular.mock.module('mApp'));
angular.mock.module(function($provide){
$provide.value('driveService', mockDriveService);
});
describe(' drive service called', function() {
beforeEach(inject(function($controller, $rootScope, $state, _$q_, driveService) {
$q = _$q_;
scope = $rootScope.$new();
mockDriveService = {
get:function(){
queryDeferred = $q.defer();
queryDeferred.resolve(driveServiceResponse);
return {$promise: queryDeferred.promise};
}
};
controller = $controller('driveCtrl', { $scope: scope, driveService:driveService});
sinon.stub(mockDriveService, 'get');
scope.$apply();
}));
it('expect filter to be empty', function () {
expect(scope.drivers).to.not.be.empty;
});
});
});
The error what i'm getting is:
Error: Unexpected request: GET /driveService/id
No more request expected
at $httpBackend (node_modules/angular-mocks/angular-mocks.js:1210:9)
at sendReq (public/javascripts/lib/angular/angular.js:10333:9)
at serverRequest (public/javascripts/lib/angular/angular.js:10045:16)
at processQueue (public/javascripts/lib/angular/angular.js:14567:28)
at public/javascripts/lib/angular/angular.js:14583:27
at Scope.$eval (public/javascripts/lib/angular/angular.js:15846:28)
at Scope.$digest (public/javascripts/lib/angular/angular.js:15657:31)
at Scope.$apply (public/javascripts/lib/angular/angular.js:15951:24)
at Context.<anonymous> (C:/Users/por/AppData/Local/Temp/b0475694b46e0d60262621ad126ce46c.browserify:63:9)
at Object.invoke (public/javascripts/lib/angular/angular.js:4450:17)
Error: Declaration Location
at window.inject.angular.mock.inject (node_modules/angular-mocks/angular-mocks.js:2375:25)

You're defining a mocked service but still providing unmocked driveService for controller (this has been already done by default).
The fact that driveService.get is stubbed after the controller was instantiated and the method was called, doesn't help the case.
It should be something like
mockDriveService = {
get: sinon.stub().returns({$promise: $q.resolve(driveServiceResponse)})
};
controller = $controller('driveCtrl', { $scope: scope, driveService:mockDriveService});
scope.$apply();
The app should have test-friendly design to be tested efficiently. Considering that router is loaded in top-level module and its configuration is defined there too, app units that are supposed to be tested should be defined in child modules, so they could be tested in isolation.
The app may be refactored as
angular.module('mApp', ['ui.router', 'mApp.drive']);
...
angular.module('mApp.drive', [])
.controller('driveCtrl', ...)
.factory('driveService', ...);
And its units may be tested like
beforeEach(angular.mock.module('mApp.drive'));
...

Related

Unknown Provider error using Karma with Angular

I have a module
export default angular.module('pfui.user', [])
.controller('ModifyUserController', ModifyUserController)
that has a controller
export default class ModifyUserController{
userid:string;
...
}
I'm trying to create a unit test that can test some methods in the controller that calls services to do some operation. This is my Karma script -
describe('ModifyUserControllerTester', function () {
var $controller;
beforeEach(angular.mock.module('ui.router'));
beforeEach(angular.mock.module('pfui.user'));
beforeEach(inject(function (_$controller_) {
$controller = _$controller_;
}));
describe('Test', function () {
it('test accessing controller', function () {
let $scope = {};
var controller = $controller('ModifyUserController', {
$scope: $scope
});
expect($scope['userid']).toBe(undefined);
});
});
});
When I run the test, I get an error
Error: [$injector:unpr] Unknown provider: UsersProvider <- Users <- ModifyUserController
Initially I was getting an error that $stateProvider was missing. So I added
beforeEach(angular.mock.module('ui.router'));
and that error went away.
This is my first attempt in writing a Karma test. I'm not sure what I am missing. Why is Karma looking for a Provider when I don't have one in the module? Any help is greatly appreciated.
Your question doesn't show any dependency injections to the ModifyUserController but going by the error you have posted it looks like you haven't provided the 'Users' Service to the controller.
describe('ModifyUserControllerTester', function () {
var $controller;
var mockUsers;
beforeEach(angular.mock.module('ui.router'));
beforeEach(angular.mock.module('pfui.user'));
beforeEach(inject(function (_$controller_) {
$controller = _$controller_;
}));
describe('Test', function () {
it('test accessing controller', function () {
//----define your mock dependency here---//
let mockUsers = jasmine.createSpyObj('mockUsers', ['user_method1',
'user_method2',...]);
let $scope = {};
var controller = $controller('ModifyUserController', {
$scope: $scope,
Users: mockUsers
});
expect($scope['userid']).toBe(undefined);
});
});
});
PS. Since its best practice for unit tests to be conducted in isolation, you should also consider providing a mock state provider vs importing the actual ui.router module

Module is not available in angularjs + Jasmine Unit testing

Module is defined as
var $manage = angular.module('manage', [....]);
Controller is defined as
$manage.line.events.controller('eventsController', [..., function(...){
$scope.page = "events";
}]);
My simple unit test case is
describe('Module: manage', function() {
beforeEach(module('manage'));
var scope, ctrl, rootScope;
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('eventsController', {
$scope: scope
});
}));
it("test page", function () {
expect(scope.page).toEqual('events');
});
});
Here, i am getting a error like
Failed to instantiate module ampleManage due to.... manage is not available.
I have integrated angular-mocks.js also. Tried so many possibilities but not working for me.
Basic need is .
Need to access controller/scope in test case.

Angular/Mocha/Chai - initialize controller for unit testing?

We're just getting started with unit testing in our Angular app and we are using a karma/mocha/chai framework for unit testing. I carried out some basic unit tests on various services and factories we have defined and it the unit testing works great. However now we want to test some controller and evaluate the scope vars that the controllers are modifying.
Here is an example of one such controller:
angular.module('App').controller('myCtrl', ['$scope', 'APIProxy',
function ($scope, APIProxy) {
$scope.caseCounts = {caseCount1: 0, caseCount2: 0};
$scope.applyCounts = function () {
$scope.caseCounts.caseCount1 = {...some case count logic...}
$scope.caseCounts.caseCount2 = {...some case count logic...}
};
APIProxy.getAll().then(function (data) {
{...do a bunch of stuff...}
$scope.data = data;
$scope.applyCounts();
});
}]
);
Now, when I unit test I would like to start off with just a simple 'does the $scope.caseCounts have values > 0, then I'll build from there. However it is not obvious how to make the controller cause the APIProxy service run and how to handle the eventual return of data. We've tried $scope.getStatus(), and $scope.apply() and a few other things but I feel like we are way off the mark and we are fundamentally missing something about how to go about this.
Currently our controller tester looks like:
describe("myCtrl unit tests",function(){
beforeEach(module('App'));
var ctrl, APIProxy;
beforeEach(inject(function ($rootScope, $controller, _APIProxy_)
{
$scope = $rootScope.$new();
APIProxy = _APIProxy_;
ctrl = $controller('myCtrl', {$scope: $scope, APIProxy: APIProxy});
}));
it('Loads data correctly', function() {
expect(ctrl).to.not.be.undefined;
//??? what else do we do here to fire the getAll function in controller?
});
});
It's usually better to test the service and the controller separately.
To test the service, you can use $httpBackend to mock the XHR requests:
https://docs.angularjs.org/api/ngMock/service/$httpBackend
To test the controller, you can simply provide mocked values instead of the actual service when you initalize the controller
APIProxy = {'mocked':'data'};
ctrl = $controller('myCtrl', {$scope: $scope, APIProxy: APIProxy});
Or more generally, to mock any provider of your module:
module(function($provide) {
$provide.constant('ab','cd');
$provide.value('ef', 'gh');
$provide.service('myService', function() { });
});
Which will override the 'myService' referenced as dependencies in your controller (if there is one). If you need it directly, you can then inject it too:
var myService;
beforeEach(inject(function (_myService_) {
myService = _myService_;
}));
If you need APIProxy to return a promise, you can mock it too with
https://docs.angularjs.org/api/ng/service/$q
and resolve, e.g.:
var deferred = $q.defer();
deferred.resolve({'mocked':'data'});
return deferred.promise;
If you do want to test them together, you can do a spy on the API function you call and have the spy return a resolved promise.

Getting "Unexpected request" error when running Karma unit test in an AngularJS app

I'm trying to write a unit test for a controller which fetches article details using $http service.
Controller:
.controller('ArticleDetailCtrl',function($scope, Article, $routeParams, API_URL, ARTICLE_URL, $http, $sce){
$scope.url = API_URL + ARTICLE_URL + '/' + $routeParams.articleId;
$http.get($scope.url).then(function(response) {
//console.log(response.data);
$scope.heading = response.data.Headline;
$scope.rawBody = response.data.Body;
$scope.body = $sce.trustAsHtml($scope.rawBody);
$scope.image = response.data.Assets[0].URL;
});
});
Unit test:
'use strict';
describe('Controller: Article', function () {
var scope,
$httpBackend,
articleEndpoint;
// load the controller's module
beforeEach(module('myApp'));
describe('ArticleDetailCtrl', function () {
var ArticleDetailCtrl,
jsonObject,
ArticleId = '123';
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, _$httpBackend_, Article, API_URL, ARTICLE_URL) {
scope = $rootScope.$new();
ArticleDetailCtrl = $controller('ArticleDetailCtrl', { $scope: scope });
$httpBackend = _$httpBackend_;
articleEndpoint = API_URL + ARTICLE_URL + '/' + ArticleId;
jsonObject = {
'Headline': 'Headline',
'Body': '<p>Body</p>',
'Assets': [
{
'URL': 'path/to/image/article1.jpg'
}
]
};
$httpBackend.when('GET', articleEndpoint).respond(jsonObject);
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('should fetch article details from the API', function () {
//expect(scope.articles.length).toEqual(3);
$httpBackend.expectGET(articleEndpoint);
$httpBackend.flush();
});
});
});
But I keep on getting the following error:
Error: Unexpected request: GET http://localhost:3000/api/articles/undefined
Expected GET http://localhost:3000/api/articles/123
at $httpBackend (/Users/gill/Documents/projects/angularjs-test/app/bower_components/angular-mocks/angular-mocks.js:1179)
at sendReq (/Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:8181)
at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:7921
at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:11319
at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:11405
at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:12412
at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular/angular.js:12224
at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular-mocks/angular-mocks.js:1438
at /Users/gill/Documents/projects/angularjs-test/test/spec/controllers/article.js:77
Error: Unsatisfied requests: GET http://localhost:3000/api/articles/123
at /Users/gill/Documents/projects/angularjs-test/app/bower_components/angular-mocks/angular-mocks.js:1472
at /Users/gill/Documents/projects/angularjs-test/test/spec/controllers/article.js:65
This is the first time am writing unit tests which I followed along by reading some tutorials. I don't know what am I doing wrong?
Your problem is a simple, but a common one.
When you write your Jasmine / Karma unit tests, creating your controllers are almost automatic using the $controller service. That means, for the most part, you can say from your unit test
$controller('ArticleDetailCtrl')
And AngularJS will figure out its dependent services, inject them and create an instance of the controller for you to unit test.
There is one exception to this:
AngularJS does not know how to inject context specific services like $scope and $routeParams, which change based on the HTML structure and the URL for each instance of the controller.
I notice in your unit test you create the controller as
ArticleDetailCtrl = $controller('ArticleDetailCtrl', { $scope: scope });
So at this point, you tell AngularJS what object to use when the controller asks for $scope as a dependent service. But in your controller, you also inject $routeParams. And based on its articleId you create the URL for the request.
So if you change your controller instantiation code to:
ArticleDetailCtrl = $controller('ArticleDetailCtrl', {
$scope: scope,
$routeParams: articleId: ArticleId
});
That should now create the correct URL (instead of using undefined for articleId, which was the error)
Let me know if that helps.

Service Injection to test controller jasmin

Newest Update
My service or factory can still not be found.
'use strict';
/* jasmine specs for controllers go here */
describe("Backpage Controller", function(){
describe('Root Ctrl', function() {
var scope, ctrl, myService;
beforeEach(module("backpageApp"));
beforeEach(inject(function(Data, $controller, $rootScope) {
myService = Data;
scope = $rootScope.$new();
ctrl = $controller("RootCtrl", {$scope:scope});
}));
it('should set the default value of orderProp model', function() {
expect(scope.orderProp).toBe('rent')
});
it('should create "listings" model with 10 listings', function() {
expect(myService.listings.length).toBe(10)
});
});
});
This is the newest error.
Chrome 32.0.1700 (Mac OS X 10.8.4) Backpage Controller Root Ctrl should create "listings" model with 10 listings FAILED
TypeError: Cannot read property 'length' of undefined
at null.<anonymous> (/Users/judyngai/Desktop/Jan2014/newback/trynewversion/minibackpage/test/unit/controllersSpec.js:24:32)
Chrome 32.0.1700 (Mac OS X 10.8.4): Executed 2 of 2 (1 FAILED) (0.284 secs / 0.058 secs)
my services.js file contain this, I am using the angular seed project.
angular.module('backpage.services', [])
.factory('Data', function($resource) {
return $resource('data/data.json');
});
my app.js file is this
var backpageApp = angular.module('backpageApp', ['ui.router', 'ngResource', 'ngRoute','backpage.services', 'backpagecontrollers']);
I am very new to angular js. I am trying to write a jasmin spec that test my RootCtrl.
I created a service to share between controllers
angular.module('backpage.services', [])
.factory('Data', function($resource) {
return $resource('data/data.json');
});
I injected the module above to my application level module.
The RootCtrl has a 'Data' parameter given to its function so the scope object can access the shared data.json fetched by $resource.
I set this to the Data in the Root Ctrl
$scope.listings = Data.query();
Now I am having trouble figuring out how to inject the service I created to my spec written in jasmin.
My Jasmin file currently looks something like this
describe("Backpage Controller", function(){
describe('Root Ctrl', function() {
var scope, ctrl;
beforeEach(module("backpageApp"));
var $injector = angular.injector(['backpageApp']);
var myService = $injector.get('backpage.services');
beforeEach(inject(function($controller, $httpBackend) {
scope = {};
ctrl = $controller("RootCtrl", {$scope:scope});
}));
it('should set the default value of orderProp model', function() {
expect(scope.orderProp).toBe('rent')
});
it('should create "listings" model with 10 listings', function() {
expect(myService.listings.length).toBe(10)
});
});
});
I am currently getting this error when I run the jasmin test
Chrome 32.0.1700 (Mac OS X 10.8.4) Backpage Controller Root Ctrl encountered a declaration exception FAILED
Error: [$injector:unpr] Unknown provider: backpage.servicesProvider <- backpage.services
I have taken a look at the testing examples on angular js official documentation, the sample uses
$httpBackend
in the jasmin code to grab the json file but the $httpBackend doc says its use with $http service but I am using resource.
Also the data.json contain 10 listings of advertisements.
any help is appreciated.
When you run beforeEach(module("backpageApp")); angular-mocks loads all the dependencies automatically and allow you to inject them as in regular applications (not tests)
You also need to create a scope with $rootScope:
var scope, ctrl, myService;
beforeEach(module("backpageApp"));
beforeEach(inject(function(Data, $controller, $httpBackend, $rootScope) {
myService = Data;
scope = $rootScope.$new();
ctrl = $controller("RootCtrl", {$scope:scope});
}));
I figured it out I should just use $httpBackend from angular js to fetch the data.json file.
I have this as a variable httpBackend
I injected this into $httpBackend
httpBackend.when('GET', 'data/data.json').respond([{id: 1 }, {id: 2}, {id:3}, {id:4}, {id:5}, {id:6}, {id:7}, {id:8}, {id:9}, {id:10}]);
and flush it back out
httpBackend.flush();
for testing purposes

Resources