简体   繁体   English

如何在控制器中测试功能?

[英]how can I test functions in my controller?

I would like to test a function called getTodaysHours in my vendor.controller. 我想在我的vendor.controller中测试一个名为getTodaysHours的函数。

vendor.controller.js vendor.controller.js

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;


//get todays hours
getTodaysHours() {
    let today = this.data.hours[new Date().getDay()];
    today.opening_time = today.substring(0,6);
    today.closing_time = today.substring(10,15);


    return (today.opening_time || '9:00am') + ' - ' + (today.closing_time || '5:00pm');
}

vendorData.service.js vendorData.service.js

export class VendorDataService {
constructor($rootScope, $resource, $q, $state, $stateParams, constant, userDataService, event, localStorageService, searchFilterService) {
    'ngInject';

    //deps
    this.$resource = $resource;
    this.$rootScope = $rootScope;
    this.$q = $q;
    this.$state = $state;
    this.$stateParams = $stateParams;
    this.searchFilterService = searchFilterService;
    this.localStorageService = localStorageService;
    this.userDataService = userDataService;
    this.constant = constant;
    this.event = event;

    //ng resource
    this.vendorResource = this.$resource('api/vendor/:vendor');
    this.vendorReviewResource = $resource('api/vendor/:id', null, {review: {method: 'PATCH'}});
    this.menuResource = this.$resource('api/vendor/menu/:id');
    this.vendorCoordinate = localStorageService.get(constant.storageKey.vendorCoordinate);

    //store current vendor
    this.currentVendor = {
        hours:[
            '10:00AM - 5:00PM',
            '9:00AM - 5:00PM',
            '9:00AM - 5:00PM',
            '9:00AM - 5:00PM',
            '9:00AM - 5:00PM',
            '9:00AM - 5:00PM',
            '10:00AM - 5:00PM'
        ]
    };

}

} }

My spec looks something like this: 我的规格看起来像这样:

describe('vendor controller', () => {
    let vm;

    beforeEach(angular.mock.module('thcmaps-ui'));
    beforeEach(inject(($controller, vendorDataService) => {
        vm = $controller('VendorController');

    }));

    it('should state store hours for today', () => {
        expect(vm.getTodaysHours).toEqual('9:00AM - 5:00PM');
    });
});

and I'm getting the following errors: 并且出现以下错误:

Error: [$injector:unpr] Unknown provider: dataProvider <- data <- VendorController 错误:[$ injector:unpr]未知提供程序:dataProvider <-数据<-VendorController

TypeError: undefined is not an object (evaluating 'vm.getTodaysHours') in /Users/adminuser/Documents/workspace/thcmaps-ui/.tmp/serve/app/index.module.js (line 9) TypeError:未定义不是/Users/adminuser/Documents/workspace/thcmaps-ui/.tmp/serve/app/index.module.js(第9行)中的对象(评估“ vm.getTodaysHours”)

  1. I'm not sure what's causing the first error. 我不确定是什么导致了第一个错误。
  2. Should I be attaching the getTodaysHours function to this ? 我应该在附加getTodaysHours函数吗? What is the proper way to do it? 正确的做法是什么?

Seems obvious, Angular can't find your data provider. 似乎很明显,Angular找不到您的data提供者。 Given it's a collaborator , you should be providing a mock / spy implementation anyway. 考虑到它是一个协作者 ,无论如何您应该提供一个模拟/间谍实现。

let vm, data;

beforeEach(() => {
    data = {
        _id: 1,
        updated_at: 'now',
        loc: {
            lng: 123,
            lat: 456
        },
        rating: 1.2
    };

    module('thcmaps-ui');
    inject($controller => {
        vm = $controller('VendorController', {
            data: data
        });
    });
});

On to you other question, getTodaysHours() is simply not a method of your controller; 关于您另一个问题, getTodaysHours()根本不是您的控制器的方法; you've defined it inside the constructor. 您已经在构造函数中定义了它。 Move it to the class body, ie 将其移至班级正文,即

class VendorController {
    constructor(...) {
        // ...
    }

    getTodaysHours() {
        // ...
    }
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Angular控制器注入单元测试 - How can I inject an Angular controller into my unit test 如何在我的子控制器中模拟此方法来测试它? - How can I mock this method in my child controller to test it? 如何在我的Karma / Mocha测试套件中测试Angular 1.5控制器? - How can I test my Angular 1.5 controller in my Karma/Mocha test suite? 我该如何测试这个Angular控制器? - How can I test this Angular controller? 如何使用Protractor / Selenium设置可用于我的测试套件的常用功能? - How can I set up common functions that are available for my test suites with Protractor / Selenium? 我怎样才能按顺序运行我的功能 - how can I run my functions be sequentially 如何在业力+茉莉花中测试我的Angular范围函数? - How do I test my Angular scope functions in karma + jasmine? 如何在特定控制器下访问资产javascript中定义的jQuery函数以适应js.erb扩展? - How can i access jquery functions defined in assets javascript foler to my js.erb extention under specific controller? 如何在SAP UI中的控制器中定义自定义控制器? - How i can define custom controller in my controller in sap ui? 使用“ controller as”语法时,如何访问控制器中的“ this”? - How can I access the “this” in my controller when using “the controller as” syntax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM