Error : Spies must be created in a before function or a spec even though its wrapped inside beforeEach

Viewed 408

I have written a jasmine test in angular 8 application and getting error Spies must be created in a before function or a spec. I tried wrapping the spies in beforeEach and the test in describe function but still getting the same error. What could be possible issue. The section here that i mentions is marked fdescribe

     describe('AgreementComponent', () => {
      let component: AgreementComponent;
      let fixture: ComponentFixture<AgreementComponent>;


      const mockAgreementsService = new Mock<AgreementsService>({
        getOutstandingAgreements: () => new Observable<AgreementsModel[]>(),
        updateAgreement: () => Promise.resolve([])
      }).Object;



      configureTestSuite(() => {
        TestBed.configureTestingModule({
          imports: [SharedModule, FontAwesomeModule],
          declarations: [AgreementComponent, CustomScrollDirective],
          providers: [{ provide: UserService, useValue: mockUserService },
          { provide: AgreementsService, useValue: mockAgreementsService }]
        });
      });  

        function setupComponent() {
    fixture = TestBed.createComponent(AgreementComponent);
    component = fixture.componentInstance;

  }


      it('should update  when decline Section is called ', () => {
        let updateSpy: jasmine.Spy;
        setupComponent();
        updateSpy = spyOn(component, 'update').withArgs(4).and.returnValue(true);
        component.declineSection();
        expect(updateSpy).toHaveBeenCalled();
      });


    fdescribe('Set Values', function () {
        const response: AgreementsModel[] = [];
        let outStandingAgreementServiceSpy: jasmine.Spy;
        let outStandingAgreementSpy: jasmine.Spy;

        beforeEach(() => {
          outStandingAgreementServiceSpy = spyOn(mockAgreementsService, 'getOutstandingAgreements').and.returnValue(of(response));
          outStandingAgreementSpy = spyOn(component, 'getOutstandingAgreements').and.callThrough();
          spyOn(component, 'calculateRemainingDaysLeft');
        });

        it('should  call getOutstandingAgreements', () => {
          setupComponent();


          component.ngOnInit();
          expect(outStandingAgreementSpy).toHaveBeenCalled();
          expect(outStandingAgreementServiceSpy).toHaveBeenCalled();
        });

      });
    });  
1 Answers

These is a chance it's because you don't need to mock out the spies this way. For the component you can set up a straight spy and the same with the service.

 fdescribe('Set Values', function () {
            const response: AgreementsModel[] = [];
            let outStandingAgreementServiceSpy: mockAgreementsService;

            beforeEach(() => {
              spyOn(mockAgreementsService, 'getOutstandingAgreements').and.returnValue(of(response));
              spyOn(component, 'getOutstandingAgreements').and.callThrough();
              spyOn(component, 'calculateRemainingDaysLeft');
            });

            it('should  call getOutstandingAgreements', () => {
              setupComponent();


              component.ngOnInit();
              expect(mockAgreementsService.getOutstandingAgreements).toHaveBeenCalled();
              expect(component.calculateRemainingDaysLeft).toHaveBeenCalled();
            });

          });

Let me know if that helps or works. An easier way is to create a jasmineSpyObj to cover the service. Here is a link which is useful for telling the difference between the two.

Related