2017-05-04 186 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); 
    }); 
}); 

保持你的配置是,你需要看以下兩個鏈接,如果您是新的測試在角:

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。所以在這種情況下:

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

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

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

當你實例化你的被測系統(SUT),要隔離所有的依賴,使你只花費你的努力測試在組件的邏輯,而不是別人。它是所有SOLID設計原則的一部分。依賴關係可以被模擬,存根,被忽略。進入所有不同的方式可以提供依賴關係,這超出了本文的範圍,但我希望這可以爲您提供一個開始。