I'm trying to write my first 'complex' test against Angular using Jasmine and Karma. This is the code that I'm trying to write a test against:
import { Component, Inject } from "@angular/core";
import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';
import { Router } from "@angular/router";
import { AuthService } from '../../services/auth.service';
@Component({
selector: "login",
templateUrl: "./login.component.html",
styleUrls: ['./login.component.css']
})
export class LoginComponent {
title: string;
form!: FormGroup;
constructor(private router: Router,
private frmbuilder: FormBuilder,
private authService: AuthService,
@Inject('BASE_URL') private baseUrl: string) {
this.title = "User Login";
this.createForm();
}
createForm() {
this.form = this.frmbuilder.group({
Email: ['', Validators.required],
Password: ['', Validators.required]
});
}…
I'm trying to write a test for the createForm method:
import { TestBed, async } from '@angular/core/testing';
import { LoginComponent } from './login.component';
import { Router } from '@angular/router';
import { AuthService } from '../../services/auth.service';
import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';
describe('create form', () => {
let component : LoginComponent;
let service : AuthService;
let router: Router;
let frmBuilder: FormBuilder;
let spy : any;
beforeEach(() => {
spyOn(component, LoginComponent.arguments(router, frmBuilder, service));
component = new LoginComponent(router, frmBuilder, service, '');
});
it('expect the login form to have been created', () => {
expect(component.createForm()).toHaveBeenCalled();
});
});
When I run the test I receive the following error: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them.
The link offered as an answer doesn't solve my issue. I think it's because I'm calling my createForm method in the constructor (perfectly valid, but hard to test). If I change the first line of my test in the beforeEach
to be : spyOn(component,'createForm');
then I receive an error could not find an object to spy upon for createForm()