我有以下的測試案例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。我不知道該怎麼做。欣賞它,如果任何人都可以幫助我,或者任何方法來做到這一點?
謝謝!我知道這個想法,但不知道該怎麼做,因爲我是茉莉花的新手。 +1的解釋也:) – Malik