0
constructor(private a:dependencyA,private  b:dependencyB,private  c:dependencyC){

}

dependencyA could look like this:

export class dependencyA {
  showPopup: boolean;
  defaultProperties = {
    showPopup: this.showPopup,
  };
  private propertiesSource = new BehaviorSubject(this.defaultProperties);
  currentProperties = this.propertiesSource.asObservable();
}

In order to be able to unit test I will have to write stubs for each constructor dependencies along with dummy data or methods within each stub by hand to make it work.

Something like:

class dependencyAStub{
  defaultProperties = {
    showPopup: false,
  };
  private propertiesSource = new BehaviorSubject(this.defaultProperties);
  currentProperties = this.propertiesSource.asObservable();
  push(value){
    this.propertiesSource.next(value);
  }
}

and,

 TestBed.configureTestingModule({
      declarations: [ ComponentDetailsComponent ],
       providers: [{ provide: dependencyA, useClass: dependencyAStub }],
providers: [{ provide: dependencyB, useClass: dependencyBStub }],
providers: [{ provide: dependencyC, useClass: dependencyCStub }],
    })

Is there a better way to provide mock stubs for all the dependencies ? If there are 8 dependencies in the constructor of the component and each one has like 4-5 functions and properties. I will have to spend a lot of time writing stubs. It would be great if I could generate stubs etc automatically and may be specify specific values for one or more dependencies manually for testing.

SamuraiJack
  • 5,131
  • 15
  • 89
  • 195
  • For the most part I would say no. What you can do instead is create a mock factory through `jasmine.createSpyObj` so that you can create mocks on the fly. –  Jun 24 '19 at 09:28
  • Are you asking about "Angular Schematics"? https://stackoverflow.com/questions/52254130/custom-project-level-templates-for-angular-components-generated-via-angular-cli – Eliseo Jun 24 '19 at 10:07

1 Answers1

1

You could try Jasmine-Mock-Factory, https://www.npmjs.com/package/jasmine-mock-factory

This library provides you the capability to not to create stubs for independent dependencies. The library also provides, decent documentation to get started.

Arun Selva Kumar
  • 2,593
  • 3
  • 19
  • 30