TypeError:使用业力运行茉莉花测试时,无法读取未定义的属性“ subscribe”

时间:2019-09-25 13:55:09

标签: angular jasmine

在使用茉莉花和业力进行以下角度测试时出现错误。我怀疑是打给 条形订单组件中的getCurrencyCodes方法。它调用tradeservice的getCurrencyCodes方法,该方法返回一个可观察值。 我做错了模拟吗?

TypeError: Cannot read property 'subscribe' of undefined

StripOrdercomponent html

 ngOnInit() {
        this.getCurrencyCodes();
    }

  private getCurrencyCodes() {
        this.tradeService.getCurrencyCodes()
            .subscribe(
            data => {
                this.currencyList = data;
            },
            error => {
                this.messageViewerModel.messages.push("Unable to get Currency codes");
            });
    }

带钢订单规格文件

class MockDateService {

}


class MockTradeService {
    getCurrencyCodes() {
        return;
    }
}

class MockAuthService {

}

class MockOrderService {

}

class MockValueDateService {

}

class MockClientService {

}


class MockMessageViewerModel {
    title: string;
    messages: string[];
   }

  @Component({
    selector: 'app-message-viewer',
    template: ''
  })
  class MockMessageViewerComponent {
    @Input()
    messageViewer: MockMessageViewerModel;    
  }

describe('StripOrderComponent', () => {
    let component: StripOrderComponent;
    let clientService: ClientService;
    let tradeService: TradeService;
    let fixture: ComponentFixture<StripOrderComponent>;


    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [
                HttpClientModule,
                RouterTestingModule,
                FormsModule,
                TranslateModule.forRoot({
                    loader: {
                        provide: TranslateLoader,
                        useClass: TranslateLanguageLoader
                    }
                })
            ],
            declarations: [
                StripOrderComponent,
                MockMessageViewerComponent,

            ],
            providers: [
                [{ provide: DateService, useClass: MockDateService }],
                [{ provide: TradeService, useClass: MockTradeService }],
                [{ provide: AuthService, useClass: MockAuthService }],
                [{ provide: OrderService, useClass: MockOrderService }],
                [{ provide: ValueDateService, useClass: MockValueDateService }],
                [{ provide: ClientService, useClass: MockClientService }]


            ],
            schemas: [NO_ERRORS_SCHEMA]

        })
            .compileComponents();
    }));

    beforeEach(() => {

        fixture = TestBed.createComponent(StripOrderComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();


    });

    it('should be created', () => {
       // spyOn(MockTradeService, 'getCurrencyCodes').and.returnValue({ subscribe: () => {} })
        expect(component).toBeTruthy();
    });


});

1 个答案:

答案 0 :(得分:0)

getCurrencyCodes必须返回Observable,以便您可以订阅它。因此,您需要返回Observable(在您的模拟或spyOn中)。几个例子:

class MockTradeService {
    getCurrencyCodes(): Observable<number> {
        return from([1, 4, 5]);
    }
}


您可以使用不同的创建者,例如of,from,interval,range ... 如果要执行模拟功能,则必须在spyOn中添加callThrough

spyOn(MockTradeService, 'getCurrencyCodes').and.callThrough();

如果要返回另一个值,请指定:

spyOn(MockTradeService, 'getCurrencyCodes').and.returnValue(from([9, 11, 7]));