我在我的控制器中有一个名为 getTodaysHours()的函数,我想测试一下。我想知道我是否可以在我的单元测试规范中模拟下面的JSON数据?
describe('vendor controller', () => {
let vm;
beforeEach(angular.mock.module('thcmaps-ui'));
beforeEach(inject(($controller) => {
vm = $controller('VendorController');
vm.data = {"_id":"56b54f9368e685ca04aa0b87","lat_lon":"33.713018,-117.841101",...}
}));
it('should state store hours for today', () => {
console.log(vm.data);
expect(1).toEqual(1);
});
});
vendor.controller
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');
}
}
当我尝试记录 vm.data 时,我正在
错误:[$ injector:unpr]未知提供者:dataProvider< - data< - VendorController
TypeError:undefined不是对象(评估'vm.data')
答案 0 :(得分:1)
在模拟模块之后,模拟JSON数据应该与 $提供常量一起使用。
let data = {"_id":"56b54f9368e685ca04aa0b87",
"lat_lon":"33.713018,-117.841101",...}
beforeEach(angular.mock.module('thcmaps-ui',
($provide) => {
$provide.constant('data', new data);
}));
老实说,我只是尝试使用函数,而不是使用JSON数据,但它应该以相同的方式工作。