2016-04-27 15 views
0

私はベンダー。コントローラでgetTodaysHoursという関数をテストしたいと思います。 「コントローラでどのように機能をテストできますか?

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'); 
    }); 
}); 

とI:

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

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' 
     ] 
    }; 

} 

}

私のスペックは次のようになります次のエラーが発生しました:

Error: [$injector:unpr] Unknown provider: dataProvider <- data <- 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)

  1. 私は、最初のエラーを引き起こしているかわからないんだけど。
  2. getTodaysHours機能をに添付する必要がありますか??それを行う正しい方法は何ですか?
+0

ご質問に** ** **関連するコードを含めてください。 – Phil

+0

更新ありがとう – Brett

答えて

1

明らかに、Angularはプロバイダdataを見つけることができません。とにかく模擬/スパイの実装を提供する必要がありますcollaboratorだとします。他の質問あなたにオン

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 
     }); 
    }); 
}); 

getTodaysHours()は、単にあなたのコントローラの方法ではありません。それをコンストラクタ内で定義しています。クラス本体に移動してください。

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

    getTodaysHours() { 
     // ... 
    } 
}  
関連する問題