Angular 4 Unit test error - Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'ng:///DynamicTestModule/LoginComponent.ngfactory.js'

前端 未结 4 1624
花落未央
花落未央 2021-02-18 15:03

I am writing some unit tests for my component and i am getting this cryptic error message. I found a similar question at Angular 2 unit testing - getting error Failed to load &#

4条回答
  •  名媛妹妹
    2021-02-18 15:40

    I just ran into this error and the problem was my mocks. In the component.ngOnInit i used this.route.paramMap.subscribe(...) where route is an ActivatedRoute instance

    In my test i provided a mock service like this :

    providers: [
        { provide: ActivatedRoute, useValue: { snapshot: { params: { id: 1 } } } }
    ]
    

    And in fact i missed to mock the paramMap method

    Then i fix it adding a paramMap properties like this

    providers: [
        { provide: ActivatedRoute, useValue: { snapshot: { params: { id: 1 } }, paramMap: Observable.of({get: () => 1}) } }
    ]
    

    Then i don't have anymore this stupid error.

    So for you, i expect the class SessionServiceStub to be incomplete or erroneous. Does it get a login method that return an Observable ?

    If it's not the problem you can check the NotificationServiceStub

    You should use a debugger (with Webstorm it's easy to debug step-by-step) to help you.

提交回复
热议问题