How to reset a spy in Jasmine?

34,994

Solution 1

const spy = spyOn(somethingService, "doSomething");

spy.calls.reset();

This resets the already made calls to the spy. This way you can reuse the spy between tests. The other way would be to nest the tests in another describe() and put a beforeEach() in it too.

Solution 2

Type 1:

var myService = jasmine.createSpyObj('MyService', ['updateSelections']);

myService.updateSelections.calls.reset();


Type 2:

var myService = spyOn(MyService, 'updateSelections');

myService.updateSelections.calls.reset();

Note: Code above is tested on Jasmine 3.5

Solution 3

The mock service with the default return value can be set in the beforeEach() , but if you want to change the mocked service response later, do not call fixture.detectChanges() in beforEach, you can call it in each spec after applying required changes (if any), if you want to change the mock service response in an specific spec, then add it to that spec before the fixture.detectChanges()

  beforeEach(() => {
    serviceSpy = jasmine.createSpyObj('RealService', ['methodName']);
    serviceSpy.methodName.and.returnValue(defaultResponse);

    TestBed.configureTestingModule({
      providers: [{provide: RealService, useValue: serviceSpy }],
    ...
    })
    ...

    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    // do not call fixture.detectChanges() here
  });

  it('test something with default mocked service response', () => {
      fixture.detectChanges();
      ....
  });

  it('test something with a new mocked service response', () => {
      serviceSpy.methodName.and.returnValue(newResponse);
      fixture.detectChanges();
      ....
  });
Share:
34,994
lorless
Author by

lorless

Technical Developer in Brighton, UK

Updated on May 29, 2020

Comments

  • lorless
    lorless about 4 years

    I have an issue where I've set up a mock service as a spy.

     mockSelectionsService = jasmine.createSpyObj(['updateSelections']);
    

    I then call that stub method twice, each time in a different test. The problem is that when i expect() the spy with .toHaveBeenCalledWith() the toHaveBeenCalledWith method also contains the arguments it was passed from the first test which produces a false positive on my second test.

    How do I wipe/clear/reset the spyObject for my next test so that it no longer believes it as been called at all?

    Initialisation of services/components

      beforeEach(() => {
        mockSelectionsService = jasmine.createSpyObj(['updateSelections']);
    
        TestBed.configureTestingModule({
          declarations: [QuickSearchComponent, LoaderComponent, SearchComponent, SearchPipe, OrderByPipe],
          providers: [OrderByPipe, SearchPipe, SlicePipe, {provide: SelectionsService, useValue: mockSelectionsService}],
          imports: [FormsModule, HttpClientModule]
        });
    
    
        fixture = TestBed.createComponent(QuickSearchComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    
        fixture.componentInstance.templates = mockTemplates;
        fixture.componentInstance.manufacturers = mockManufacturers;
      });
    
    • Akanksha Gaur
      Akanksha Gaur over 5 years
      add your spy object initialization in beforeEach
    • lorless
      lorless over 5 years
      I've tried re-initialising the object with mockSelectionsService = jasmine.createSpyObj(['updateSelections']); in the describe's beforeEach() but this does not seem to work. I've noted that there seem be a lot of different ways to create spies but the jasmine documentation/api is not exactly forthcoming with answers. I've updated my answer to show the initialisation in my first beforeEach()
  • lorless
    lorless over 5 years
    I get Cannot read property 'reset' of undefined when attempting mockSelectionsService.calls.reset() in my beforeEach()
  • chirag.sweng
    chirag.sweng over 5 years
    As you are creating spy object you need to call something like below for reset mockSelectionsService.method.calls.reset();
  • Maddy
    Maddy about 4 years
    as of serviceSpy.and.returnValue(newResponse) is working