true/falseを返す関数をテストしたいが、単体テストの場合はコンポーネントの一部ではない。 specファイルにそのリファレンスをどのように取得するのですか?角度2の単位テストでコンポーネントの一部ではない関数にアクセスする方法は?
ダミー部品:
function Validate(abc){
}
@Directive({
selector: [abc-credit]
})
export class CreditDirective {
this.valid=Validate(abc);
}
specファイル:
import {CreditDirective} from './credit.directive';
describe('CreditDirective',() => {
let component: TestLayoutComponent;
let fixture: ComponentFixture<TestLayoutComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [CreditDirective, TestLayoutComponent],
imports: [FormsModule, UtilitiesModule, BrowserAnimationsModule]
})
.compileComponents().then(() => {
fixture = TestBed.createComponent(TestLayoutComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
}));
it('should properly execute Validate()', function(){
expect(validate(cardType)).toBeTruthy();
});
});
@Component({
selector: 'test-layout',
template: 'some template'
})
export class TestLayoutComponent {
}
検証は()TestLayoutComponent又はCardDirectiveのいずれかの一部ではありません。では、テストケースでどのようにアクセスするのですか?
をエクスポートする必要があり、専用のspecファイルを作成し、機能をインポートすることができます(abc){'そして、あなたがあなたのテストで' import {CreditDirective、Validate} 'を使って './credit.directive'からインポートすることができます; – nemesv
@nemesvうまくいきました。ありがとうございました:) 私はそれが承認されたとマークすることができるようにあなたの答え:) –