Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何用jasmine测试函数?_Angular_Ionic Framework_Jasmine_Ionic2 - Fatal编程技术网

Angular 如何用jasmine测试函数?

Angular 如何用jasmine测试函数?,angular,ionic-framework,jasmine,ionic2,Angular,Ionic Framework,Jasmine,Ionic2,我有一个带构造函数的类和一些类: import {Weight} from './weight'; export class Weight { constructor(public viewCtrl: ViewController, public navCtrl: NavController, private user: User, private zone: NgZone, private alert: AlertPopupServices){ } getSomeVal

我有一个带构造函数的类和一些类:

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

单元测试的常用公式是排列、动作、断言。因此,在这种情况下:

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

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

//Assert
expect(result).toEqual(10);
当您实例化测试中的系统(sut)时,您希望隔离它的所有依赖项,以便您只需要花费精力测试组件中的逻辑,而不是其他人。这一切都是坚实的设计原则的一部分。依赖项可以被模拟、存根或忽略。讨论提供依赖关系的所有不同方式超出了本文的范围,但我希望这能给您一个开始

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);
    });
});
保持配置不变,如果您不熟悉角度测试,则需要查看以下两个链接:


您在测试中导入了类权重吗?是的,我导入了
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);
    });
});