title: Returning 2 different values using using a mock service while unit testing in Angular
body: <p>Summary of the issue :</p>
  
  <p><strong>How to call multiple values using the same service inside the same test spec and check to see if it works exactly as in the component?</strong></p>
  
  <p>I am using Angular7+.</p>
  
  <p>Let there be a component ( say A ) with a service injected into it.</p>
  
  <p>Suppose , there is a <strong>getNumber</strong> function in the same <strong>service</strong> with 2 parameters namely <strong>( "Key" , "Value" )</strong> where "Key" can only be either <strong>num1</strong> or <strong>num2</strong> and "Value" can be any <strong>integer</strong>.</p>
  
  <p>This function returns an object in this format : <strong>{ "Key" : num1 or num2 , "Value" : number }</strong> and this object is stored in another object ( say <strong>Total</strong> ) as its elements. Example : </p>
  
  <pre><code>export class A implements OnInit{
   Total={"sum":["num1","num2"],"amount":0};
  
   constructor(private service: SomeService) {}
      ngOnInit(){
          this.loadFunc();
      }
  
      private loadFunc(){
          this.service.getNumber("num1",12).subscribe(res =&gt;{
                  Total[res.Key] = res.Value;
          },err=&gt;{
              console.log(err);
          });
  
          this.service.getNumber("num2",13).subscribe(res =&gt;{
                  Total[res.Key] = res.Value;
          },err=&gt;{
              console.log(err);
          });
  
          this.calculate();
      }
  
      private calculate(){
          //For elements in Total.sum array iterate over them and store the value in any other key say amount.
          for(const keys of Total["sum"]){
              if(Total[keys]){
                  Total["amount"] += Total[keys];
              }
          }
          console.log(Total["amount"]);      // Outputs to 25
      }
  }
  
  So Total would become : 
  Total = { "num1":12 , "num2":13 ,"sum":["num1","num2"],"amount":25};
  </code></pre>
  
  <p>Now in <strong>unit testing</strong> the component with the service, i have a mockService stub with a getNumber function and i am doing something like this :</p>
  
  <pre><code>mockServiceStub = jasmine.createSpyObj(['getNumber']); // done before testbed.createComponent()
  
  // Inside Test
  mockServiceStub.getNumber.and.returnValue(of({"Key":"num1", "Value":12}));
  fixture.ngOnInit();
  console.log(component.Total.amount); // outputs 12
  
  mockServiceStub.getNumber.and.returnValue(of({"Key":"num2", "Value":13}));
  fixture.ngOnInit();
  console.log(component.Total.amount); // outputs 13
  expect(component.Total.sum).toEqual(25); // throws error 'Expected 13 to equal 25'
  </code></pre>
  
  <p>I actually wanted both the values to combine i.e in my test,
  i first gave a return value of 12 with " num1 " as key and then 13 with " num2 " as key, and i expected the output to be 25 ( 12 + 13 ).</p>