如何用茉莉测试功能



我有一个带有创造因子的课程和一些类:

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);
    });
});

但是如何在IT中调用该函数并获取返回值?

谢谢

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);
    });
});

保持配置原样,如果您是Angular测试的新手,则需要查看以下两个链接:

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

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

请注意,该类具有注射剂:

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部分添加注入:

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

单位测试的通常公式是安排,ACT,断言。因此,在这种情况下:

//Arrange
var systemUnderTest: Weight = new Weight(mockViewController, mockNavController, mockUser, etc)
//Act
var result = systemUnderTest.getSomeValue(1,2);
//Assert
expect(result).toEqual(10);

当您实例化测试系统(SUT(时,您想隔离它的所有依赖项,以便您仅花费您的努力来测试组件中的逻辑,而不是某人。它是固体设计原理的一部分。依赖项可以被嘲笑,固执,忽略。可以提供所有不同的依赖性方式,但我希望这超出了本文的范围。

相关内容

  • 没有找到相关文章

最新更新