angularjs - How to mock $scope.variables in jasmine -


i have following test case companyctrlspec.js

describe('viewcompanyctrl', function () {     var $rootscope, scope, $controller , $q ;     beforeeach(angular.mock.module('myapp'));     beforeeach(inject(function ($rootscope, $controller ) {         scope = $rootscope.$new();         createcontroller = function() {             return $controller('viewcompanyctrl', {             $scope: scope,             company : {}                 });          };     }));      it('the company type should equal object', function () {         var controller = new createcontroller();         //some assertion     }); }); 

following viewcompanyctrl.js file

angular.module('myapp').controller('viewcompanyctrl',     function ($scope, companyservice, $state, meetingservice, company, attachmentservice) {         'use strict';          $scope.company = company;          $scope.companyinfo = {};         $scope.companyinfo['aname'] = [$scope.company.address.street, $scope.company.address.zipcode + ' ' + $scope.company.address.city].join(', ');        //more code       }); 

following app.routes.js file company getting resolved

.state('company', {             abstract: true,             url: '/company/:companyid',             resolve: {                 company: function($q, $stateparams, companyservice){                     var deferred = $q.defer();                      companyservice                         .getcompany($stateparams.companyid)                         .error(function(data, status, headers){                             //more code                         })                         .success(function(data){                             deferred.resolve(data);                         });                      return deferred.promise;                 }             }, 

my problem following error

        typeerror: $scope.company.address undefined in c:/users/myapp/webapirole/app/compan y/viewcompanyctrl.js (line 8)         @c:/users/myapp/webapirole/app/company/viewcompanyctrl.js:8:42 

i guessing happens because didn't mock scope.company.address in test case . not sure how . appreciate if 1 can me , or method ?

it looks me $scope.company same company injected controller. need set address on company injecting mock, so:

beforeeach(inject(function ($rootscope, $controller ) {     scope = $rootscope.$new();     createcontroller = function() {         return $controller('viewcompanyctrl', {             $scope: scope,             company : {                 address: {/* address data goes here */}             }         });      }; })); 

if want company data different each test, pass createcontroller() function:

beforeeach(inject(function ($rootscope, $controller ) {     scope = $rootscope.$new();     createcontroller = function(company) {         return $controller('viewcompanyctrl', {             $scope: scope,             company : company         });      }; }));  it('the company type should equal object', function () {     var company = {address: {/* address data goes here */}};     var controller = new createcontroller(company);     //some assertion }); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -