Do I need to unsubscribe from subscriptions in my unit tests?

后端 未结 1 1459
独厮守ぢ
独厮守ぢ 2021-02-19 04:26

If I have a test such as the following:

it(\'should return some observable\', async(() => {
  mockBackend.connections.subscribe((mockConnection: MockConnectio         


        
1条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-19 05:11

    @estus is correct, you do not need to unsubscribe.

    tl;dr;

    • Observables/Subjects hold refererences to all subscribers until complete.
    • Objects with active references cannot be marked for Garbage collection. Basic JS.
    • Http Observables do complete, disgarding subscriptions at that time, and freeing them up for GC.

    • Until the Observable/Subject gets torn down or completes, no subscription can be GC'd unless you unsubscribe.

    Consider this simple Angular TestBed setup:

    describe(`DataService`, () => {
      let service: DataService;
      let httpMock: HttpTestingController;
    
      beforeEach(() => {
        TestBed.configureTestingModule({
          imports: [HttpClientTestingModule],
          providers: [
            DataService,
          ]
        });
    
        service = TestBed.get(DataService);
        httpMock = TestBed.get(HttpTestingController);
      });
    
      it(`should get a response`, () => {
        service.getData().subscribe(response => {
          expect(response).toBeTruthy();
        });
      });
    

    In the case of an Http request via the Angular HttpClient, the Observable will complete, and the references to Subscribers will be released for GC. In the case of general Angular Jasmine Testing as shown above--even if you had an Observable that didn't complete--the DataService object being referenced by service gets reset every time beforeEach is called, and the old DataService object will have lost all references and is marked for GC. Once that happens, any subscriptions referenced by the Observable/Subject in the DataService will lose references to them, and they'll be GC'd as well.

    Remember, memory leaks happen with Observables because they retain references to all Subscribers and unless the Subscriber unsubscribes. They will continue to have valid references to them and their containing functions, and cannot be marked for GC until The Subject/Observable is destroyed. If the Subject continues to live and you keep adding subscribers--such as new instance of the same Subscriber--you will keep adding a bunch of old, unused objects to memory.

    0 讨论(0)
提交回复
热议问题