2015-06-01 133 views
3

我有以下的測試案例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 be equal to an object', function() { 
     var controller = new createController(); 
     //some assertion 
    }); 
}); 

以下是ViewCompanyCtrl.js文件

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 


    }); 

以下是app.routes.js文件其中公司是越來越解析

.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; 
       } 
      }, 

我p roblem的是,我得到以下錯誤

 TypeError: $scope.company.Address is undefined in C:/Users/MyApp/WebApiRole/app/compan 
y/ViewCompanyCtrl.js (line 8) 
     @C:/Users/MyApp/WebApiRole/app/company/ViewCompanyCtrl.js:8:42 

我猜測,這是因爲我沒有在我的測試案例嘲笑scope.company.Address。我不知道該怎麼做。欣賞它,如果任何人都可以幫助我,或者任何方法來做到這一點?

回答

4

它看起來像$scope.companycompany是一樣的,它被注入到控制器中。所以,你只需要設置在companyAddress,你是注入你的模仿,像這樣:

beforeEach(inject(function ($rootScope, $controller) { 
    scope = $rootScope.$new(); 
    createController = function() { 
     return $controller('ViewCompanyCtrl', { 
      $scope: scope, 
      company : { 
       Address: {/* address data goes here */} 
      } 
     }); 
    }; 
})); 

如果你希望公司的數據爲每個測試有所不同,簡單地把它傳遞到您的createController()功能:

beforeEach(inject(function ($rootScope, $controller) { 
    scope = $rootScope.$new(); 
    createController = function(company) { 
     return $controller('ViewCompanyCtrl', { 
      $scope: scope, 
      company : company 
     }); 
    }; 
})); 

it('the company type should be equal to an object', function() { 
    var company = {Address: {/* address data goes here */}}; 
    var controller = new createController(company); 
    //some assertion 
}); 
+0

謝謝!我知道這個想法,但不知道該怎麼做,因爲我是茉莉花的新手。 +1的解釋也:) – Malik

1

嘗試將控制器添加到路線的定義中。控制器不是像其他服務那樣的單身人士。它通常與一個國家或一個觀點聯繫在一起。

.state('company', { 
     abstract: true, 
     url: '/company/:companyId', 
     controller: 'ViewCompanyCtrl' 
     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; 
      } 
     }, 

更妙的是我會用controller As約定,而不是在你的控制器使用範圍。那麼你的控制器將會成爲範圍。 除此之外,我強烈建議看看John Papa's coding standardTodd Motto's。兩者都很好,並會推薦使用該慣例。