SimonTestNet / SimonTest

Repository for SimonTest questions and issues https://simontest.net
16 stars 2 forks source link

Private method calls generated #6

Closed intellix closed 7 years ago

intellix commented 7 years ago

Ran against a form component with a private method and realised SimonTest generates method calls against privates, which is impossible. Typically I'm testing private methods indirectly. I also noticed that ngOnInit tests weren't generated.

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';

@Component({
  selector: 'xc-blah',
  templateUrl: './blah.component.html',
  styleUrls: ['./blah.component.scss'],
})
export class BlahComponent implements OnInit {

  form: FormGroup;

  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    this.createForm();
  }

  private createForm() {
    this.form = this.fb.group({ });
  }
}

Generates:

import { ComponentFixture, TestBed } from "@angular/core/testing";
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { FormBuilder } from "@angular/forms";
import { BlahComponent } from "./responsible-gaming.component";

describe("BlahComponent", () => {
  let comp: BlahComponent;
  let fixture: ComponentFixture<BlahComponent>;
  let formBuilderStub: any;

  beforeEach(() => {
    formBuilderStub = {
      group: () => ({})
    };
    TestBed.configureTestingModule({
      declarations: [BlahComponent],
      schemas: [NO_ERRORS_SCHEMA],
      providers: [
        { provide: FormBuilder, useValue: formBuilderStub }
      ]
    });
    fixture = TestBed.createComponent(BlahComponent);
    comp = fixture.componentInstance;
  });

  it("can load instance", () => {
    expect(comp).toBeTruthy();
  });

  describe("createForm", () => {
    it("makes expected calls", () => {
      spyOn(formBuilderStub, "group");
      comp.createForm();
      expect(formBuilderStub.group).toHaveBeenCalled();
    });
  });

});
ManuelDeLeon commented 7 years ago

Pick up v0.11.2

The reason ngOnInit is ignored is because it doesn't call a public method (only a private one).

    ngOnInit() {
        this.createForm();
        this.doSomething();
    }
    private createForm() {
        this.form = this.fb.group({});
    }
    doSomething() {

    }

generates:

    ngOnInit() {
        this.createForm();
        this.doSomething();
    }
    private createForm() {
        this.form = this.fb.group({});
    }
    doSomething() {

    }