3

I'm writing my first Angular tests and having a problem here. I'm testing a component, which has a custom attribute in it

<ng-container *isGranted="admin">
...
</ng-container>

Official documentation says

The NO_ERRORS_SCHEMA tells the Angular compiler to ignore unrecognized elements and attributes.

So I added the following to my TestBed config:

schemas: [NO_ERRORS_SCHEMA]

But still, I'm getting an error:

Property binding isGranted not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations

Where am I wrong? I do not want to test how isGranted behaves, I just want to check that a correct value was assigned to it.

Majesty
  • 2,097
  • 5
  • 24
  • 55
  • 1
    At first, it looks like your `isGranted`-directive is not added to your TestBed configurations. Try adding it as a declaration or provider. – John Aug 06 '19 at 06:31
  • @John when I adding this to declaration the compiler trying to initialize the attribute and throws an error that service (the one that is used inside the attribute) is missing – Majesty Aug 06 '19 at 06:36
  • 1
    I am not sure if that is the reason, but did you try something like this? `TestBed.configureTestingModule({ declarations: [isGranted] });` You also need to provide the services you use in the isGranted-directive inside your TestBed configuration as well. – John Aug 06 '19 at 06:40
  • I do not want to provide the service, that's the point. Anyway, this is how the official doc says – Majesty Aug 06 '19 at 06:42
  • Have you tried to provide the services as well? Maybe use a Mock-service if the service in itself is not applicable for testing. Even though you want to isolate testing, if the isGranted-directive is using some services in order for it to work, you at least need to provide a mock if I am not mistaken. – John Aug 06 '19 at 06:43
  • I do believe that if I mock the service behavior this will break the concept of unit testing, because if I change the service in future, this particular test stay green, because service is mocked. – Majesty Aug 06 '19 at 06:45
  • NO_ERRORS_SCHEMA - ignores custom component, while you are using ng-container which is not custom :) – Vova Bilyachat Aug 06 '19 at 06:46
  • @VolodymyrBilyachat good point, but changing ng-container to div did not change anything – Majesty Aug 06 '19 at 06:47
  • 1
    @LuninRoman because of directive, so my point is error schema is to ignore custom components but not directives – Vova Bilyachat Aug 06 '19 at 06:48
  • On a second thought, since you are not testing the isGranted directive, you can mock that one instead? Something like this? `declarations: [{provide: isGranted, useValue: {}}]` – John Aug 06 '19 at 06:51
  • 1
    @John no its directive and not provider :) – Vova Bilyachat Aug 06 '19 at 07:03

1 Answers1

2

You are using directive, while NO_ERROR_SCHEMA is to ignore custom components. See github when error is thrown with proper exception saying that you need to include directives

Property binding ${prop.name} not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations".
Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80