1

I am getting an error while running the following angular test while using jasmine and karma. I have mocked the service dependencies of the component. However there is function of the tradeservice that is being used in the component. I have tried mocking the function but it still complains

TypeError: this.tradeService.getCurrencyCodes is not a function

What is the problem ?

In the component code

constructor(private tradeService: TradeService,
        private authService: AuthService,
        private orderService: OrderService,
        private valueDateService: ValueDateService,
        private dateService: DateService,
        private clientService: ClientService) {
    }

 ngOnInit() {
        this.getCurrencyCodes();
    }


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

In the spec file of the component

 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, useValue: MockDateService }],
                [{ provide: TradeService, useValue: MockTradeService }],
                [{ provide: AuthService, useValue: MockAuthService }],
                [{ provide: OrderService, useValue: MockOrderService }],
                [{ provide: ValueDateService, useValue: MockValueDateService }],
                [{ provide: ClientService, useValue: MockClientService }]
            ],
            schemas: [NO_ERRORS_SCHEMA]

        })
            .compileComponents();
    }));

    beforeEach(() => {

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


    });

    it('should be created', () => {

        expect(component).toBeTruthy();
    });


});

TradeService

getCurrencyCodes() {
        return this.tradeEndpoint.getCurrencyCodesEndpoint<string[]>();
    }
2
  • Do you need these lines at the top of your test or can they be omitted? let clientService: ClientService; let tradeService: TradeService; Commented Sep 25, 2019 at 10:36
  • can be ommitted Commented Sep 25, 2019 at 10:41

2 Answers 2

1

Try to change useValue into useClass in following line.

[{ provide: TradeService, useValue: MockTradeService }],
Sign up to request clarification or add additional context in comments.

Comments

0

try to delete fixture.detectChanges inside your beforeEach, it's probably the cause why your test fails, detectChanges will only work for onPush events

beforeEach(() => {
    fixture = TestBed.createComponent(StripOrderComponent);
    component = fixture.componentInstance;
    fixture.detectChanges(); // delete this
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.