使用Jasmine返回TypeError的单元测试:不是函数

时间:2018-05-16 20:50:45

标签: angular jasmine

我正在尝试使用Jasmine进行单元测试并调用我的Angular 5服务,但是我收到了以下错误

TypeError: this.dailySalesService.resetValues is not a function

调用changeDate中的方法component.changeDate(new Date());时会发生这种情况。在changeDate方法中,我正确地调用了dailySalesService,所以我认为我的组件中不是错误。

我的单元测试

import 'rxjs/add/observable/of';
import { Observable } from 'rxjs/Observable';
import { LayoutService } from './../../core/services/layout.service';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { HomeDailySalesComponent } from './home-daily-sales.component';
import { HomeDailySalesProgressComponent } from './home-daily-sales-progress.component';
import { DailySalesService } from '../shared/daily-sales.service';
import { TextMaskModule } from 'angular2-text-mask';
import { FormsModule } from '@angular/forms';

describe('Component: HomeDailySalesComponent', () => {
    let component: HomeDailySalesComponent;
    let fixture: ComponentFixture<HomeDailySalesComponent>;
    const dailySalesServiceMock = jasmine.createSpyObj('DailySalesService', ['getSummarySalesDay']);

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [
                TextMaskModule,
                FormsModule
            ],
            providers: [
                { provide: DailySalesService, useValue: dailySalesServiceMock }
            ],
            declarations: [
                HomeDailySalesComponent,
                HomeDailySalesProgressComponent
            ]
        });

        fixture = TestBed.createComponent(HomeDailySalesComponent);
        component = fixture.componentInstance;
    });

    it('should return service success without content', () => {
        dailySalesServiceMock.getSummarySalesDay.and.callFake(function () {
            return Observable.of({});
        });

        // error here, when changeDate is called
        component.changeDate(new Date());
        expect(component.error).toBeTruthy();
        expect(dailySalesServiceMock.getSummarySalesDay).toHaveBeenCalledTimes(2);
    });
});

日志错误

at <Jasmine>
    at HomeDailySalesComponent.webpackJsonp../src/app/home/home-daily-sales/home-daily-sales.component.ts.HomeDailySalesComponent.changeDate (http://localhost:9876/_karma_webpack_/webpack:/src/app/home/home-daily-sales/home-daily-sales.component.ts:53:56)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/app/home/home-daily-sales/home-daily-sales.component.spec.ts:105:19)
    at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:388:1)
    at ProxyZoneSpec.webpackJsonp../node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/proxy.js:128:1)
    at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:387:1)
    at Zone.webpackJsonp../node_modules/zone.js/dist/zone.js.Zone.run (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:138:1)
    at runInTestZone (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/jasmine-patch.js:145:1)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/jasmine-patch.js:160:1)
    at <Jasmine>

0 个答案:

没有答案