コンストラクタにコードを追加しましたが、ユニットテストに失敗しました。新しいコードは、観察からthis.isFollowOnMode
にtrueまたはfalseに設定します。ジャスミンで観測値をユニットテストする方法
import { CustomerGroupService } from 'customerGroup.service';
class HeaderBarController {
private isFollowOnMode: boolean;
constructor(private customerGroupService: CustomerGroupService) {
'ngInject';
//new code that is causing test to fail:
this.customerGroupService.isFollowOnMode$ // will be true or false
.subscribe((isFollowOnMode) => {
this.isFollowOnMode = isFollowOnMode;
});
}
// other stuff
}
export default HeaderBarController;
私は私のユニットテストでは、次のエラーを取得:
PhantomJS 2.1.1 (Mac OS X 0.0.0) Controller: HeaderBarController should be defined FAILED TypeError: undefined is not a constructor (evaluating 'this.customerGroupService.isFollowOnMode$'
そして、これが失敗している私のユニットテストです:
describe('Controller: HeaderBarController', function() {
beforeEach(angular.mock.module(module.name));
beforeEach(angular.mock.module(function ($provide) {
$provide.service('customerGroupService',() => { });
}));
beforeEach(inject(function ($rootScope, $componentController) {
this.$scope = $rootScope.$new();
this.ctrl = $componentController('headerBar',
{
// locals
$scope: this.$scope,
$element: [{}],
$attrs: [],
},
{
// scope bindings
}
);
}));
it('should be defined', function() {
expect(this.ctrl).toBeDefined();
});
});
だから、私はthis.customerGroupService.isFollowOnMode$
を設定していないようです。そのデフォルト値はfalseでなければなりません。単体テストで私は単体テストに慣れていないので、どんな助けも素晴らしいでしょう。ありがとう。
物事が変更されない限り、TypeScript/ESNextソースコードの '' ngInject''は信頼できないことがわかっています。 'HeaderBarController {static $ inject = ['customerGroupService'];」と書くと、より堅牢になるでしょう。代わりに、デコレータを使って注釈を自動的に作成することができます。 –