2017-11-26 2 views
0

I는 다음과 같은 몇 가지 링크가있는 구성 요소를 테스트하기 위해 노력하고있어 : 또한, 이미 this question로보고 시도각도 2 단위 테스트 routerLink 클릭

<a routerLink="/contact">Contact</a> 

하지만이 해결되지 않았다 문제. 나는 그 질문 (아래 코드)에 의해 제안 된 솔루션을하려고하면 내가 공급자를 설정하는 트라이 경우

, 나는,이 오류

beforeEach(async(() => { 
    TestBed.configureTestingModule({ 
     imports: [RouterTestingModule], 
     declarations: [FooterComponent], 
    }).compileComponents(); 
})); 

beforeEach(() => { 
    fixture = TestBed.createComponent(FooterComponent); 
    component = fixture.componentInstance; 
}); 

describe('...',() => { 
    it('should navigate to "/contact" when clicking on link', async(inject([Location], (location: Location) => { 
     fixture.debugElement.query(By.css('.menu')).children[1].nativeElement.click(); 
     fixture.detectChanges(); 
     fixture.whenStable().then(() => { 
      console.log(location.path()); 
     }); 
    }))); 
}); 

No provider for Location!

가 계속 : [위치] 나는이 오류가 발생합니다 :

spyOn(router, 'navigate'); 
expect(router.navigate).toHaveBeenCalled(); 
01 :

Failed: StaticInjectorError[LocationStrategy]:
StaticInjectorError[LocationStrategy]:
NullInjectorError: No provider for LocationStrategy!

나는 다음과 같이 라우터 스파이 시도

하지만 '절대로 호출하지 않음'으로 테스트에 실패합니다.

이 문제를 어떻게 해결할 수 있습니까?

+0

'CommonModule'이 누락되었다고 생각합니다. 사이드 노트는 상상할 수있는 가장 보일러가 많은 상용 코드 중 일부입니다. –

+0

@AluanHaddad 오류를 해결했지만 location.path() 로그는 예상 URL 대신 항상 공백으로 표시합니다. – celsomtrindade

+0

나는 그것에 대해 전혀 모른다. 그래도 설정이 의심스러워 보입니다. 살펴 봐야 할 다양한 동기 및 비동기 초기화 로직이 있습니다. –

답변

1

의견에서 Aluan Haddad가 지적한대로 importsCommonModules이 누락되었습니다.

그 후 오류는 사라졌지만 제대로 작동하지 못했습니다. 이유는 내가 MockModule을 사용했기 때문입니다. 여기서는 모든 공통 서비스, 구성 요소, 모듈 등을 가져옵니다. RouterTestingModule을 포함하여 테스트에서 사용되었습니다.

MockModule에서 RouterTestingModule을 제거하고 imports으로 직접 이동하면 문제가 해결됩니다.

// before 
imports: [MockModule], 

// after 
imports: [RouterTestingModule.withRoutes(routing)], 
+0

많은 사람들이 도움이 될 것으로 생각합니다. 이런 식으로 왜 작동하는지 몇 가지 밝힐 수 있습니까? 그것은 매우 임의적으로 보입니다. –

관련 문제