2017-12-22 29 views
0

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のいずれかの一部ではありません。では、テストケースでどのようにアクセスするのですか?

+0

をエクスポートする必要があり、専用のspecファイルを作成し、機能をインポートすることができます(abc){'そして、あなたがあなたのテストで' import {CreditDirective、Validate} 'を使って './credit.directive'からインポートすることができます; – nemesv

+0

@nemesvうまくいきました。ありがとうございました:) 私はそれが承認されたとマークすることができるようにあなたの答え:) –

答えて

1

あなたが孤立してその機能をテストすることができるようにあなたは、もちろん、あなたはまた、 `エクスポート機能の検証を使用して検証関数をエクスポートする必要がある機能

+0

それは機能をエクスポートするだけで働いた。ありがとうございました :) –

関連する問題