jensengar
jensengar

Reputation: 6167

Unit Tests when using uirouter angular 2+

I'm relatively new to ng2+, but I haven't been able to find any example about how to run unit/integration tests while using uirouter. I have a basic sanity test that looks like this:

describe('LoginSignupComponent', () => {
  let component: LoginSignupComponent;
  let fixture: ComponentFixture<LoginSignupComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        LoginComponent,
        LoginSignupComponent,
        CreateUserComponent,
        ValidationComponent,
        WidgetLoadingComponent
      ],
      imports: [
        ReactiveFormsModule,
        InputMaskModule,
        HttpClientTestingModule,
        NgbModule.forRoot(),
        UIRouterModule.forRoot({})
      ],
      providers: [
        LoginService,
        LoginStateService,
        StateService,
        {provide: APP_BASE_HREF, useValue : '/' },
      ]
    })
    .compileComponents();
  }));

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

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

But it fails with the following: Error: Can't resolve all parameters for StateService: (?).

Where StateService is coming from uirouter. How do I fix this? I couldn't find any documentation on uirouter about this.

Upvotes: 2

Views: 628

Answers (2)

Simon Stanford
Simon Stanford

Reputation: 501

I found that it was enough to just reference the UI Router module in the imports section:

 await TestBed.configureTestingModule({
  declarations: [ HomeComponent ],
  imports: [
    UIRouterModule.forRoot({}), 
    HttpClientModule,
    ModalModule.forRoot()
  ],
  providers: [
    {provide: 'environment', useValue: environment},
  ]
})

Upvotes: 1

Mezo Istvan
Mezo Istvan

Reputation: 2782

This error message means that there is one parameter in the constructor of StateService that is not present in the current TestBed. Open the implementation of StateService, check its constructor and provide the missing dependency to the TestBed. If that dependency needs to be mocked or it's just not important for the test, use a fake implementation with useValue or useClass in the providers array.

Upvotes: 0

Related Questions