Javascript 如何检查注入控制器中的功能?
我目前正在尝试测试我的控制器上的getTodaysHours函数是否被调用。最终,该函数应该从模拟JSON数据中获得数小时的时间,并在参数匹配时传递,但我仍停留在第一部分 供应商控制器Javascript 如何检查注入控制器中的功能?,javascript,angularjs,json,unit-testing,jasmine,Javascript,Angularjs,Json,Unit Testing,Jasmine,我目前正在尝试测试我的控制器上的getTodaysHours函数是否被调用。最终,该函数应该从模拟JSON数据中获得数小时的时间,并在参数匹配时传递,但我仍停留在第一部分 供应商控制器 export class VendorController { constructor($rootScope, data, event, toastr, moment, _, distanceService, vendorDataService, userDataService, stateManager
export class VendorController {
constructor($rootScope, data, event, toastr, moment, _, distanceService, vendorDataService, userDataService, stateManagerService) {
'ngInject';
//deps
this.$rootScope = $rootScope;
this.toastr = toastr;
this._ = _;
this.userDataService = userDataService;
this.vendorDataService = vendorDataService;
this.stateManagerService = stateManagerService;
this.event = event;
//bootstrap
data.isDeepLink = true;
this.data = data;
this.data.last_update = moment(this.data.updated_at).format('MM/DD/YY h:mm A');
this.data.distance = distanceService.getDistance(this.data.loc.lng, this.data.loc.lat);
this.data.todaysHours = this.getTodaysHours();
this.data.rating_num = Math.floor(data.rating);
this.hasReviewed = (userDataService.user.reviewed[data._id]) ? true : false;
this.isGrid = false;
this.isSearching = false;
this.hideIntro = true;
this.menuCollapsed = true;
this.filterMenuCollapsed = true;
this.selectedCategory = 'All';
this.todaysHours = '';
this.type = '';
this.searchString = '';
this.reviewScore = 0;
this.today = new Date().getDay();
this.vendorDataService.currentVendor = data;
//load marker onto map
$rootScope.$broadcast(event.ui.vendor.pageLoad, data);
//get menu
vendorDataService.getVendorMenu(data._id)
.then((res)=> {
this.data.menu = res.menu;
this.menuContainer = this.data.menu;
this.totalResults = this.getTotalResults();
this.availableMenuCategories = this.getAvailableMenuCategories();
})
.catch(() => {
this.toastr.error('Whoops, Something went wrong! We were not able to load the menu.', 'Error');
});
}
//get todays hours
getTodaysHours() {
let today = this.data.hours[new Date().getDay()];
return (today.opening_time || '9:00am') + ' - ' + (today.closing_time || '5:00pm');
}
}
当我用$provide常量模拟JSON数据时,第一个测试通过
然后我尝试注入控制器(不确定语法是否正确):
它给了我某种形式的错误。第二个测试在计算vm.getTodaysHours()时给出了一个未定义的错误:
PhantomJS 2.1.1(Mac OS X 0.0.0)供应商控制器应通过测试失败
forEach@/Users/adminuser/Documents/workspace/thcmaps ui/bower_components/angular/angular.js:341:24
loadModules@/Users/adminuser/Documents/workspace/thcmaps ui/bower_components/angular/angular.js:4456:12
createInjector@/Users/adminuser/Documents/workspace/thcmaps ui/bower_components/angular/angular.js:4381:22
workFn@/Users/adminuser/Documents/workspace/thcmaps ui/bower_components/angular mocks/angular mocks.js:2507:60
/Users/adminuser/Documents/workspace/thcmaps ui/bower_components/angular/angular.js:4496:53
PhantomJS 2.1.1(Mac OS X 0.0.0)供应商控制器应调用getTodaysHours失败
forEach@/Users/adminuser/Documents/workspace/thcmaps ui/bower_components/angular/angular.js:341:24
loadModules@/Users/adminuser/Documents/workspace/thcmaps ui/bower_components/angular/angular.js:4456:12
createInjector@/Users/adminuser/Documents/workspace/thcmaps ui/bower_components/angular/angular.js:4381:22
workFn@/Users/adminuser/Documents/workspace/thcmaps ui/bower_components/angular mocks/angular mocks.js:2507:60
/Users/adminuser/Documents/workspace/thcmaps ui/bower_components/angular/angular.js:4496:53
TypeError:undefined不是/Users/adminuser/Documents/workspace/thcmaps ui/.tmp/service/app/index.module.js(第9行)中的对象(评估“vm.getTodaysHours”)
/Users/adminuser/Documents/workspace/thcmaps ui/.tmp/service/app/index.module.js:9:244419
使用
$controller
实例化控制器时,需要注入控制器的依赖项。例如,考虑下面的控制器:
class MyController {
constructor($rootScope, $log) {
// Store the controllers dependencies
this.$rootScope = $rootScope;
this.$log = $log;
}
// Return obituary value from the $rootScope
getValue() {
this.$log.debug('Retrieving value');
return this.$rootScope.foobar;
}
// Get the current date
getDate() {
this.$log.debug('Getting date');
return Date.now()
}
static get $inject() {
return ['$scope', '$log'];
}
}
我使用ES6编写了这个控制器,请注意,依赖项是在类声明底部的static$inject
getter中定义的。这将由AngularJS在实例化时获取
如您所见,控制器依赖于$rootScope
和$log
提供程序。出于测试目的模拟此控制器时,必须按如下方式注入控制器依赖项:
describe('Spec: MyController', () => {
var controller;
beforeEach(inject(($rootScope, $log, $controller) => {
controller = $controller('MyController', {
$rootScope,
$log
});
});
it('should return a value from the $rootScope', () => {
var value = controller.getValue();
// ... perform checks
});
it('should return the current date', () => {
var date = controller.getDate();
// ... perform checks
});
});
Jasmine的最新版本使开发人员能够在整个测试过程中利用this
关键字
每个之前的任何声明、每个之后的任何声明和它
声明都将共享对该的相同引用,从而避免创建封闭变量(如上文所示,var controller
),也避免创建不必要的全局变量。例如:
beforeEach(inject(function ($rootScope, $log, $controller) {
this.controller = $controller('MyController', {
$rootScope,
$log
});
});
it('should return a value from the $rootScope', function () {
this.value = controller.getValue();
// ... perform checks
});
请注意对$controller
的调用中的第二个参数,它必须是一个包含您的控制器('MyController',在本例中)所依赖的预期依赖项的对象
这背后的理由只是允许开发人员将模拟服务、工厂、提供者等传递给控制器,作为间谍的替代方案
:$controller
:此
块,doh!)
感谢Iain,我注入了依赖项,但仍然得到了与以前相同的错误。每个错误现在看起来都是这样的:请您添加问题中遇到的错误,上面的代码应该可以正常工作。最有可能的问题是源代码,而不是测试。如果您正确地模拟了控制器,那么您应该得到第二个错误。这就是为什么会出现第一个错误,但我不确定。您确定控制器定义中没有错误吗?如果你能在Plunker中重现这个错误,我可以帮你解决,否则,你只能靠自己了。
describe('Spec: MyController', () => {
var controller;
beforeEach(inject(($rootScope, $log, $controller) => {
controller = $controller('MyController', {
$rootScope,
$log
});
});
it('should return a value from the $rootScope', () => {
var value = controller.getValue();
// ... perform checks
});
it('should return the current date', () => {
var date = controller.getDate();
// ... perform checks
});
});
beforeEach(inject(function ($rootScope, $log, $controller) {
this.controller = $controller('MyController', {
$rootScope,
$log
});
});
it('should return a value from the $rootScope', function () {
this.value = controller.getValue();
// ... perform checks
});