Skip to content

How do I test a directive that requires a service provided by the host? #3753

Discussion options

You must be logged in to vote

Hi @Simon-TheHelpfulCat,

you can provide MyService in MockRender, in this case it will be considered as Host injection: https://codesandbox.io/s/upbeat-ioana-fzqdkj?file=/src/test.spec.ts:474-534

import {Directive, Host, Injectable,} from '@angular/core';
import {MockBuilder, MockProvider, MockRender} from 'ng-mocks';

@Injectable()
class MyService {}

@Directive({
  selector: "[myDirective]",
  standalone: true
})
export class MyDirective {
  constructor(@Host() public readonly service: MyService) {}
}

describe('my sandbox', () => {
  beforeEach(() => MockBuilder(MyDirective));

  it("should create", () => {
    const fixture = MockRender(MyDirective, null, {
      providers: [
        M…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Simon-TheHelpfulCat
Comment options

Answer selected by Simon-TheHelpfulCat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants