1

I have a method that inside I have a condition that I want to test. Something like:

myMethod():void {
  let a:boolean;
  a = this.myService.getValue();

  if (a) { 
    doThis();
  if (!a) { 
    doThat();

so in my it I did something like:

it("Should check if doThis called", () => {
     spyOn(myService, "getValue").andReturn(true);
     component.myMethod();
     expect ...
});

But the condition wouldn't work and a would never get the "fake" value I mock with the spy

My question is if there's any way I can access / assign value to the local variable of myMethod() , a , or the only way is to make it global and then I can do in the it something like component.a =

Thanks.

3
  • Why not pass this.myService.getValue() as a parameter of myMethod? Commented Jul 26, 2019 at 3:59
  • Could be an idea, but right now this what the method looks like Commented Jul 26, 2019 at 4:48
  • a would never get the "fake" value I mock with the spy ?? Why is that ? Commented Jul 26, 2019 at 10:54

1 Answer 1

2
let a;
spyOn(myService,"getValue").and.callFake(function(data){
      a=data;
      expect(data).not.toBeNull();
component.myMethod();
    })

Try this may be help you

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.