Zineb Sid
Zineb Sid

Reputation: 1

Angular Unit Testing : Expect undefined to equal true

I am new in programming and this is the first time im asked to do unit testing in Angular and im a lil bit confused... I want to test this method in my component.ts:

isInputHidden = true;

showInput(){this.isInputHidden = false;}

spec.ts :

it('should show the input', () => {
component.isInputHidden == false;
let showInput = component.showInput();
expect(showInput).toBe(true);

})

When i run this test i get these errors : In jasmine ==> Expect undefined to equal true. In Terminal ==> Argument of type 'true' is not assignable to parameter of type 'Expected'. Someone can help me to figure out what should i change?

Upvotes: 0

Views: 3345

Answers (1)

Gérôme Grignon
Gérôme Grignon

Reputation: 4238

Here is how to fix your test :

  • initialize your variable with = and not ==
  • call directly the function (you can't intiialize the showInput variable with it anyway as the function doesn't return anything)
  • the expected variable to test is InputHidden directly as you changed its value with the showInput() function
it('should show the input', () => {
component.isInputHidden = false; // removed a "="
component.showInput(); // your function will change isInputHidden directly
expect(component.isInputHidden).toBe(true); // test isInputHidden

Upvotes: 2

Related Questions