0

Part of service class:

@Injectable()
export class ErrorHelper {
  constructor(private schemaValidator: SchemaValidator, private translate: TranslateService) {
  }

Test:

class TranslateServiceMock {

}

describe('ErrorHelper', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        {provide: SchemaValidator, useValue: {}},
        {provide: TranslateService, useClass: TranslateServiceMock},
        ErrorHelper
      ]
    });
  });

  it('is injected', async(inject([ErrorHelper], (errorHelper: ErrorHelper) => {
    expect(errorHelper).toBeDefined();
  })));
});

Error:

Failed: Can't resolve all parameters for ErrorHelper: (?, TranslateService).

I don't get it. How it can fail on DI when it doesn't even have to use real classes (I have supplied all dependencies)?

I have a few similar tests and DI works fine in them, I am really confused about this error.

If I remove ErrorHelper and its injection, that SchemaValidator mock is is being injected to tests just fine.

monnef
  • 3,903
  • 5
  • 30
  • 50
  • Are you sure that you're importing service directly from it's own file? – yurzui Dec 19 '16 at 12:31
  • If you mean importing `SchemaValidator` in `ErrorHelper` then I am importing it like this: `import { SchemaValidator } from './schema-validator.service';`. The validator is also being injected in its own tests and works, not sure why it fails in this case. – monnef Dec 19 '16 at 12:37
  • Can you improve this plunker to reproduce your problem? https://plnkr.co/edit/K4pcGI1EIVaSBDEMiXH4?p=preview – yurzui Dec 19 '16 at 12:39
  • I mean that your `SchemaValidator` might be imported from different file I usually solve this kind of problem by deep debugging. If you have a github repo it would be great – yurzui Dec 19 '16 at 12:47
  • Unfortunately I cannot share the code and I am unable to reproduce the problem in that plunk :-(. I am using webpack (slightly modified this seed - https://github.com/preboot/angular2-webpack/), maybe it's linked to some problem in a building process? I'll try to look at compiled test files. Nevertheless thank you for your time. – monnef Dec 19 '16 at 13:31
  • I am starting to think there is some circular import issue in ErrorHelper file, I'll split it to multiple files and hope for the best. – monnef Dec 19 '16 at 13:48
  • 1
    Check also this http://stackoverflow.com/questions/40826073/pass-parameter-to-mddialog-in-angular-material-2/40828423#40828423 – yurzui Dec 19 '16 at 13:49
  • 1
    One more solution here http://stackoverflow.com/questions/40525850/circular-dependency-injection-angular-2/40525992#40525992 – yurzui Dec 19 '16 at 13:52

0 Answers0