mardi 6 septembre 2016

How to write test in angularjs according to john papa style guide

I am new to writing test in angularjs.I have a controller which i have written according to john papa style guide.I need to access those methods in the controller in my test case.How do i do it?I am using jasmine.My sample code is:

(function(){
  angular.module('myapp')
  .controller('AlertsController', AlertsController);
AlertsController.$inject = ['$scope', 'StorageFactory']
function AlertsController($scope, StorageFactory) {
  var vm=this;
  vm.createAlert=createAlert();
  function createAlert(){
    //function code
    }  
})

and my test is as follows:

 'use strict';
 describe('Controller: AlertsController', function() {
var AlertsController, scope, StorageFactory;

beforeEach(module('myapp'));
beforeEach(inject(function(_$controller_, _StorageFactory_,      _$rootScope_) {
    scope = _$rootScope_.$new();
    AlertsController = _$controller_;
    StorageFactory = _StorageFactory_;
}));

var createController = function() {
    AlertsController('AlertsController', {
        '$scope': scope,
        'StorageFactory': StorageFactory
    });

};
 it('should create an alert', function() {
    spyOn(StorageFactory, 'isLoggedIn').and.callFake(function() {
        return true;
    });
    createController();
    expect(AlertsController).toBeDefined();
    AlertsController.createAlert();  
});
});

Am getting the following error:

TypeError: undefined is not a constructor (evaluating 'AlertsController.createAlert()') in /Users/Ramya/Documents/panitr_mobile/test/spec/controllers/AlertsCtrlSpec.js (line 57)
    at /Users/Ramya/Documents/panitr_mobile/test/spec/controllers/AlertsCtrlSpec.js:57:37
PhantomJS 2.0.0 (Mac OS X 0.0.0): Executed 2 of 2 (1 FAILED) (0 secs / 0.242 secPhantomJS 2.0.0 (Mac OS X 0.0.0): Executed 2 of 2 (1 FAILED) (0.054 secs / 0.242 secs)

Aucun commentaire:

Enregistrer un commentaire