2017-05-04 6 views
-2

私はコンストラクタといくつかのクラスを持つクラスを持っている:ジャスミンで関数をテストするには?

import {Weight} from './weight'; 
export class Weight { 
    constructor(public viewCtrl: ViewController, public navCtrl: NavController, private user: User, private zone: NgZone, private alert: AlertPopupServices){ 
    } 
    getSomeValue(a,b){ 
     return a + b; 
    } 
} 

私はジャスミンとそれをテストしようとしています。

describe('BLABLABLA',() => { 
    TestBed.configureTestingModule({ 

     declarations: [MyApp, Weight], 

     providers: [ 
      SecureStorageServices, NavController, User, AlertPopupServices, 
      {provide: ViewController, useClass: ViewControllerMock}, 
     ], 

     imports: [ 
      IonicModule.forRoot(MyApp), TranslateModule.forRoot({ 
       loader: { 
        provide: TranslateLoader, 
        useFactory: (createTranslateLoader), 
        deps: [Http] 
       } 
      }) 
     ] 

    }).compileComponents(); 
    it('has to give the addition of two numbers',() => { 
     expect("FUNCTION CALL").toEqual(10); 
    }); 
}); 

しかし、どのように私はそれで関数を呼び出し、戻り値を得ることができますか?クラスは、注射を持っていることを

おかげ

+0

あなたはクラスの重量をインポートしましたあなたのテストで? –

+0

はい、私はそれを行います – anubis

答えて

2
import { ComponentFixture, TestBed } from '@angular/core/testing'; 

import { Weight } from './weight'; 
describe('BLABLABLA',() => { 
    let comp: Weight; 
    beforeEach(() => { 
     TestBed.configureTestingModule({ 
     declarations: [ BannerComponent ], // declare the test component 
     fixture = TestBed.createComponent(Weight); 
     comp = fixture.componentInstance; // Weight test instance 
    }); 

    it('has to give the addition of two numbers',() => { 
     expect(comp.getSomeValue(5,5)).toEqual(10); 
    }); 
}); 

であるとして、あなたは角にテストを初めて使用する場合は、次の2つのリンクを見てする必要があり、あなたの設定をしてください:

https://angular.io/docs/ts/latest/guide/testing.html

https://jasmine.github.io/2.4/introduction.html

1

注:

import {Weight} from './weight'; 
import { Injectable } from '@angular/core'; 
@Injectable() 
export class Weight { 
    constructor(public viewCtrl: ViewController, public navCtrl: NavController, private user: User, private zone: NgZone, private alert: AlertPopupServices){ 
    } 
    getSomeValue(a,b){ 
     return a + b; 
    } 
} 

テストベッド

TestBed.configureTestingModule({ 

     declarations: [MyApp, Weight], 

     providers: [ 
      SecureStorageServices, NavController, User, AlertPopupServices, 
      {provide: ViewController, useClass: ViewControllerMock}, 
      Weight 
     ], 

     imports: [ 
      IonicModule.forRoot(MyApp), TranslateModule.forRoot({ 
       loader: { 
        provide: TranslateLoader, 
        useFactory: (createTranslateLoader), 
        deps: [Http] 
       } 
      }) 
     ] 

    }).compileComponents(); 

のプロバイダでクラスを入れて、それのセクションに注入を追加します。

it('has to give the addition of two numbers', inject([Weight], (weight) => { 
    // call functions using weight variable 
}); 
1
it('has to give the addition of two numbers', async(inject([Weight], (weight: Weight) => { 
     expect(weight.getSomeValue(2,3)).toEqual(5); 
}))); 
1

ユニットテストの通常の式は、Arrange、Act、Assertです。 SOこの場合には:あなたがテスト(SUT)の下で、あなたのシステムをインスタンス化するとき

//Arrange 
var systemUnderTest: Weight = new Weight(mockViewController, mockNavController, mockUser, etc) 

//Act 
var result = systemUnderTest.getSomeValue(1,2); 

//Assert 
expect(result).toEqual(10); 

、あなたは、あなたのコンポーネントとよそではない誰かのロジックをテストするあなたの努力を費やしているように、その依存関係のすべてを隔離したいです。 SOLIDのすべての主要なプリンシパルの一部です。依存関係は、嘲笑され、スタブされ、無視される可能性があります。依存関係を提供することができるさまざまな方法に進むことは、この記事の範囲を超えていますが、これがあなたにスタートを与えることを願っています。

関連する問題