Shailesh Ladumor
Shailesh Ladumor

Reputation: 7242

How i can test local variable in Angular 2 with karma + jasmine?

I am trying to write a unit test with jasmine. The issue I am facing is how to test a local variable using jasmine. I was able to test a global variable but not a local variable.

Here is my function:

checkDayIsOpen(): void {
  let isSpecial = false;
     this.items.forEach((item) => {
         if (item.is_open) {
             isSpecial = true;
         }
      });
   if(isSpeciality){
       call another function here...
     }
}

I want to test the value of isSpecial.

Upvotes: 4

Views: 11758

Answers (1)

Robin Dijkhof
Robin Dijkhof

Reputation: 19278

You can't test it because it is gone after the call. However, you can test if that other fucntion has been called.

describe("Person toString() Test", function() {
  it("calls the getName() function", function() {
    var testPerson = new Person();
    spyOn(testPerson, "getName");
    testPerson.toString();
    expect(testPerson.getName).toHaveBeenCalled();
  });
});

Upvotes: 4

Related Questions