Angular 6 - Unit testing a subscribe function in constructor










1














I've been trying to unit test the subscribe function of this service. And looking at the code coverage report generated by istanbul, I can see that this code is not covered.



Code



layout.component.ts



import Component, HostListener, Input from '@angular/core';

import LayoutService from './layout.service';

import some from 'lodash';

@Component(
selector: 'cgm-layout',
templateUrl: './layout.component.html',
styleUrls: ['./layout.component.scss'],
providers: [LayoutService]
)
class LayoutComponent

message: any;

constructor(
private service: LayoutService
)
service.messagePublished$.subscribe(
message =>
this.setMessage(message);

);


setMessage(message): void
this.message = message;
setTimeout(() =>
this.message = null;
, 7000);




export
LayoutComponent
;


This is my Unit Test



layout.component.spec.ts



import CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA from '@angular/core';
import async, ComponentFixture, fakeAsync, TestBed, tick from '@angular/core/testing';
import of from 'rxjs';
import LayoutComponent from './layout.component';
import LayoutService from './layout.service';

describe('LayoutComponent', () =>
let component: LayoutComponent;
let fixture: ComponentFixture<LayoutComponent>;
let service;

beforeEach(async(() =>
service = new LayoutService();
mockLayoutService = jasmine.createSpyObj('LayoutService', ['messagePublished$']);
TestBed.configureTestingModule(
declarations: [
LayoutComponent,

],
providers: [
LayoutService
],
schemas: [
NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA
]
)
.compileComponents();
));

beforeEach(() =>
fixture = TestBed.createComponent(LayoutComponent);
component = fixture.componentInstance;
fixture.detectChanges();

component.message = 'Garbage';
);

it('should call messagePublished', () =>
spyOn(service.messagePublished$, 'subscribe');
TestBed.createComponent(LayoutComponent);

expect(service.messagePublished$.subscribe).toHaveBeenCalled();
);

describe('setMessage', () =>

it('should set the Message', fakeAsync(() =>
component.setMessage('Message');

expect(component.message).toBe('Message');
tick(7000);
expect(component.message).toBeNull();
));

);
);


So the code never seems to go over the 'service.messagePublished$.subscribe' part. Here is the code coverage report.



The error I'm getting is 'Expected spy subscribe to have been called', which I'm guessing is the error you get when that code block is not covered.










share|improve this question




























    1














    I've been trying to unit test the subscribe function of this service. And looking at the code coverage report generated by istanbul, I can see that this code is not covered.



    Code



    layout.component.ts



    import Component, HostListener, Input from '@angular/core';

    import LayoutService from './layout.service';

    import some from 'lodash';

    @Component(
    selector: 'cgm-layout',
    templateUrl: './layout.component.html',
    styleUrls: ['./layout.component.scss'],
    providers: [LayoutService]
    )
    class LayoutComponent

    message: any;

    constructor(
    private service: LayoutService
    )
    service.messagePublished$.subscribe(
    message =>
    this.setMessage(message);

    );


    setMessage(message): void
    this.message = message;
    setTimeout(() =>
    this.message = null;
    , 7000);




    export
    LayoutComponent
    ;


    This is my Unit Test



    layout.component.spec.ts



    import CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA from '@angular/core';
    import async, ComponentFixture, fakeAsync, TestBed, tick from '@angular/core/testing';
    import of from 'rxjs';
    import LayoutComponent from './layout.component';
    import LayoutService from './layout.service';

    describe('LayoutComponent', () =>
    let component: LayoutComponent;
    let fixture: ComponentFixture<LayoutComponent>;
    let service;

    beforeEach(async(() =>
    service = new LayoutService();
    mockLayoutService = jasmine.createSpyObj('LayoutService', ['messagePublished$']);
    TestBed.configureTestingModule(
    declarations: [
    LayoutComponent,

    ],
    providers: [
    LayoutService
    ],
    schemas: [
    NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA
    ]
    )
    .compileComponents();
    ));

    beforeEach(() =>
    fixture = TestBed.createComponent(LayoutComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();

    component.message = 'Garbage';
    );

    it('should call messagePublished', () =>
    spyOn(service.messagePublished$, 'subscribe');
    TestBed.createComponent(LayoutComponent);

    expect(service.messagePublished$.subscribe).toHaveBeenCalled();
    );

    describe('setMessage', () =>

    it('should set the Message', fakeAsync(() =>
    component.setMessage('Message');

    expect(component.message).toBe('Message');
    tick(7000);
    expect(component.message).toBeNull();
    ));

    );
    );


    So the code never seems to go over the 'service.messagePublished$.subscribe' part. Here is the code coverage report.



    The error I'm getting is 'Expected spy subscribe to have been called', which I'm guessing is the error you get when that code block is not covered.










    share|improve this question


























      1












      1








      1







      I've been trying to unit test the subscribe function of this service. And looking at the code coverage report generated by istanbul, I can see that this code is not covered.



      Code



      layout.component.ts



      import Component, HostListener, Input from '@angular/core';

      import LayoutService from './layout.service';

      import some from 'lodash';

      @Component(
      selector: 'cgm-layout',
      templateUrl: './layout.component.html',
      styleUrls: ['./layout.component.scss'],
      providers: [LayoutService]
      )
      class LayoutComponent

      message: any;

      constructor(
      private service: LayoutService
      )
      service.messagePublished$.subscribe(
      message =>
      this.setMessage(message);

      );


      setMessage(message): void
      this.message = message;
      setTimeout(() =>
      this.message = null;
      , 7000);




      export
      LayoutComponent
      ;


      This is my Unit Test



      layout.component.spec.ts



      import CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA from '@angular/core';
      import async, ComponentFixture, fakeAsync, TestBed, tick from '@angular/core/testing';
      import of from 'rxjs';
      import LayoutComponent from './layout.component';
      import LayoutService from './layout.service';

      describe('LayoutComponent', () =>
      let component: LayoutComponent;
      let fixture: ComponentFixture<LayoutComponent>;
      let service;

      beforeEach(async(() =>
      service = new LayoutService();
      mockLayoutService = jasmine.createSpyObj('LayoutService', ['messagePublished$']);
      TestBed.configureTestingModule(
      declarations: [
      LayoutComponent,

      ],
      providers: [
      LayoutService
      ],
      schemas: [
      NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA
      ]
      )
      .compileComponents();
      ));

      beforeEach(() =>
      fixture = TestBed.createComponent(LayoutComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();

      component.message = 'Garbage';
      );

      it('should call messagePublished', () =>
      spyOn(service.messagePublished$, 'subscribe');
      TestBed.createComponent(LayoutComponent);

      expect(service.messagePublished$.subscribe).toHaveBeenCalled();
      );

      describe('setMessage', () =>

      it('should set the Message', fakeAsync(() =>
      component.setMessage('Message');

      expect(component.message).toBe('Message');
      tick(7000);
      expect(component.message).toBeNull();
      ));

      );
      );


      So the code never seems to go over the 'service.messagePublished$.subscribe' part. Here is the code coverage report.



      The error I'm getting is 'Expected spy subscribe to have been called', which I'm guessing is the error you get when that code block is not covered.










      share|improve this question















      I've been trying to unit test the subscribe function of this service. And looking at the code coverage report generated by istanbul, I can see that this code is not covered.



      Code



      layout.component.ts



      import Component, HostListener, Input from '@angular/core';

      import LayoutService from './layout.service';

      import some from 'lodash';

      @Component(
      selector: 'cgm-layout',
      templateUrl: './layout.component.html',
      styleUrls: ['./layout.component.scss'],
      providers: [LayoutService]
      )
      class LayoutComponent

      message: any;

      constructor(
      private service: LayoutService
      )
      service.messagePublished$.subscribe(
      message =>
      this.setMessage(message);

      );


      setMessage(message): void
      this.message = message;
      setTimeout(() =>
      this.message = null;
      , 7000);




      export
      LayoutComponent
      ;


      This is my Unit Test



      layout.component.spec.ts



      import CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA from '@angular/core';
      import async, ComponentFixture, fakeAsync, TestBed, tick from '@angular/core/testing';
      import of from 'rxjs';
      import LayoutComponent from './layout.component';
      import LayoutService from './layout.service';

      describe('LayoutComponent', () =>
      let component: LayoutComponent;
      let fixture: ComponentFixture<LayoutComponent>;
      let service;

      beforeEach(async(() =>
      service = new LayoutService();
      mockLayoutService = jasmine.createSpyObj('LayoutService', ['messagePublished$']);
      TestBed.configureTestingModule(
      declarations: [
      LayoutComponent,

      ],
      providers: [
      LayoutService
      ],
      schemas: [
      NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA
      ]
      )
      .compileComponents();
      ));

      beforeEach(() =>
      fixture = TestBed.createComponent(LayoutComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();

      component.message = 'Garbage';
      );

      it('should call messagePublished', () =>
      spyOn(service.messagePublished$, 'subscribe');
      TestBed.createComponent(LayoutComponent);

      expect(service.messagePublished$.subscribe).toHaveBeenCalled();
      );

      describe('setMessage', () =>

      it('should set the Message', fakeAsync(() =>
      component.setMessage('Message');

      expect(component.message).toBe('Message');
      tick(7000);
      expect(component.message).toBeNull();
      ));

      );
      );


      So the code never seems to go over the 'service.messagePublished$.subscribe' part. Here is the code coverage report.



      The error I'm getting is 'Expected spy subscribe to have been called', which I'm guessing is the error you get when that code block is not covered.







      javascript angular unit-testing karma-jasmine






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 at 9:36

























      asked Nov 12 at 10:34









      Simba3696

      267




      267






















          1 Answer
          1






          active

          oldest

          votes


















          1














          I'd advise you to move your subscription from the constructor to an ngOnInit. Angular created several lifecycle hooks which get called when a component get's created (ngOnInit) and other when data changes or when it gets destroyed - see Angular lifecycle hooks.



          This way you can test your code by calling the ngOnInit() method.



          In case you cannot change the code you can try creating a component instance and check if your method was called like in pseudo-code below:



          import CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA from '@angular/core';
          import async, ComponentFixture, fakeAsync, TestBed, tick from '@angular/core/testing';
          import of from 'rxjs';
          import LayoutComponent from './layout.component';
          import LayoutService from './layout.service';

          describe('LayoutComponent', () =>
          let component: LayoutComponent;
          let fixture: ComponentFixture<LayoutComponent>;
          let serviceSpy: jasmine.SpyObj<LayoutService>;;

          beforeEach(async(() =>
          const spy = spyOn(service.messagePublished$, 'subscribe')
          TestBed.configureTestingModule(
          declarations: [
          LayoutComponent,

          ],
          providers: [
          provide: LayoutService, useValue: spy
          ],
          schemas: [
          NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA
          ]
          )
          .compileComponents();
          serviceSpy = TestBed.get(ValueService);
          ));

          beforeEach(() =>
          fixture = TestBed.createComponent(LayoutComponent);
          component = fixture.componentInstance;
          fixture.detectChanges();

          component.message = 'Garbage';
          );

          it('should call messagePublished', () =>
          TestBed.createComponent(LayoutComponent);

          expect(service.messagePublished$.subscribe).toHaveBeenCalled();
          );

          );





          share|improve this answer






















          • That's a good idea but I'm only in charge of unit testing right now. So I can't really change the code. I have to figure out a way to access the code when it's in the constructor.
            – Simba3696
            Nov 12 at 12:14










          • Updated the code for testing constructor tests. Hopefully it should give you an idea to how to check if the code has been run.
            – Mac_W
            Nov 12 at 12:21










          • It is because you replace your LayoutService with a mock in this line: provide: LayoutService, useValue: mockLayoutService and mockLayoutService doesn't have a property of messagePublished$. mockLayoutService needs to be an object with a property messagePublished$ which has a subscribe property.
            – Mac_W
            Nov 12 at 14:44











          • Do you want to update your code? I'll try and help
            – Mac_W
            Nov 13 at 8:34










          • Code updated. I hope it gives you a clearer picture.
            – Simba3696
            Nov 13 at 9:08










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260326%2fangular-6-unit-testing-a-subscribe-function-in-constructor%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          I'd advise you to move your subscription from the constructor to an ngOnInit. Angular created several lifecycle hooks which get called when a component get's created (ngOnInit) and other when data changes or when it gets destroyed - see Angular lifecycle hooks.



          This way you can test your code by calling the ngOnInit() method.



          In case you cannot change the code you can try creating a component instance and check if your method was called like in pseudo-code below:



          import CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA from '@angular/core';
          import async, ComponentFixture, fakeAsync, TestBed, tick from '@angular/core/testing';
          import of from 'rxjs';
          import LayoutComponent from './layout.component';
          import LayoutService from './layout.service';

          describe('LayoutComponent', () =>
          let component: LayoutComponent;
          let fixture: ComponentFixture<LayoutComponent>;
          let serviceSpy: jasmine.SpyObj<LayoutService>;;

          beforeEach(async(() =>
          const spy = spyOn(service.messagePublished$, 'subscribe')
          TestBed.configureTestingModule(
          declarations: [
          LayoutComponent,

          ],
          providers: [
          provide: LayoutService, useValue: spy
          ],
          schemas: [
          NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA
          ]
          )
          .compileComponents();
          serviceSpy = TestBed.get(ValueService);
          ));

          beforeEach(() =>
          fixture = TestBed.createComponent(LayoutComponent);
          component = fixture.componentInstance;
          fixture.detectChanges();

          component.message = 'Garbage';
          );

          it('should call messagePublished', () =>
          TestBed.createComponent(LayoutComponent);

          expect(service.messagePublished$.subscribe).toHaveBeenCalled();
          );

          );





          share|improve this answer






















          • That's a good idea but I'm only in charge of unit testing right now. So I can't really change the code. I have to figure out a way to access the code when it's in the constructor.
            – Simba3696
            Nov 12 at 12:14










          • Updated the code for testing constructor tests. Hopefully it should give you an idea to how to check if the code has been run.
            – Mac_W
            Nov 12 at 12:21










          • It is because you replace your LayoutService with a mock in this line: provide: LayoutService, useValue: mockLayoutService and mockLayoutService doesn't have a property of messagePublished$. mockLayoutService needs to be an object with a property messagePublished$ which has a subscribe property.
            – Mac_W
            Nov 12 at 14:44











          • Do you want to update your code? I'll try and help
            – Mac_W
            Nov 13 at 8:34










          • Code updated. I hope it gives you a clearer picture.
            – Simba3696
            Nov 13 at 9:08















          1














          I'd advise you to move your subscription from the constructor to an ngOnInit. Angular created several lifecycle hooks which get called when a component get's created (ngOnInit) and other when data changes or when it gets destroyed - see Angular lifecycle hooks.



          This way you can test your code by calling the ngOnInit() method.



          In case you cannot change the code you can try creating a component instance and check if your method was called like in pseudo-code below:



          import CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA from '@angular/core';
          import async, ComponentFixture, fakeAsync, TestBed, tick from '@angular/core/testing';
          import of from 'rxjs';
          import LayoutComponent from './layout.component';
          import LayoutService from './layout.service';

          describe('LayoutComponent', () =>
          let component: LayoutComponent;
          let fixture: ComponentFixture<LayoutComponent>;
          let serviceSpy: jasmine.SpyObj<LayoutService>;;

          beforeEach(async(() =>
          const spy = spyOn(service.messagePublished$, 'subscribe')
          TestBed.configureTestingModule(
          declarations: [
          LayoutComponent,

          ],
          providers: [
          provide: LayoutService, useValue: spy
          ],
          schemas: [
          NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA
          ]
          )
          .compileComponents();
          serviceSpy = TestBed.get(ValueService);
          ));

          beforeEach(() =>
          fixture = TestBed.createComponent(LayoutComponent);
          component = fixture.componentInstance;
          fixture.detectChanges();

          component.message = 'Garbage';
          );

          it('should call messagePublished', () =>
          TestBed.createComponent(LayoutComponent);

          expect(service.messagePublished$.subscribe).toHaveBeenCalled();
          );

          );





          share|improve this answer






















          • That's a good idea but I'm only in charge of unit testing right now. So I can't really change the code. I have to figure out a way to access the code when it's in the constructor.
            – Simba3696
            Nov 12 at 12:14










          • Updated the code for testing constructor tests. Hopefully it should give you an idea to how to check if the code has been run.
            – Mac_W
            Nov 12 at 12:21










          • It is because you replace your LayoutService with a mock in this line: provide: LayoutService, useValue: mockLayoutService and mockLayoutService doesn't have a property of messagePublished$. mockLayoutService needs to be an object with a property messagePublished$ which has a subscribe property.
            – Mac_W
            Nov 12 at 14:44











          • Do you want to update your code? I'll try and help
            – Mac_W
            Nov 13 at 8:34










          • Code updated. I hope it gives you a clearer picture.
            – Simba3696
            Nov 13 at 9:08













          1












          1








          1






          I'd advise you to move your subscription from the constructor to an ngOnInit. Angular created several lifecycle hooks which get called when a component get's created (ngOnInit) and other when data changes or when it gets destroyed - see Angular lifecycle hooks.



          This way you can test your code by calling the ngOnInit() method.



          In case you cannot change the code you can try creating a component instance and check if your method was called like in pseudo-code below:



          import CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA from '@angular/core';
          import async, ComponentFixture, fakeAsync, TestBed, tick from '@angular/core/testing';
          import of from 'rxjs';
          import LayoutComponent from './layout.component';
          import LayoutService from './layout.service';

          describe('LayoutComponent', () =>
          let component: LayoutComponent;
          let fixture: ComponentFixture<LayoutComponent>;
          let serviceSpy: jasmine.SpyObj<LayoutService>;;

          beforeEach(async(() =>
          const spy = spyOn(service.messagePublished$, 'subscribe')
          TestBed.configureTestingModule(
          declarations: [
          LayoutComponent,

          ],
          providers: [
          provide: LayoutService, useValue: spy
          ],
          schemas: [
          NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA
          ]
          )
          .compileComponents();
          serviceSpy = TestBed.get(ValueService);
          ));

          beforeEach(() =>
          fixture = TestBed.createComponent(LayoutComponent);
          component = fixture.componentInstance;
          fixture.detectChanges();

          component.message = 'Garbage';
          );

          it('should call messagePublished', () =>
          TestBed.createComponent(LayoutComponent);

          expect(service.messagePublished$.subscribe).toHaveBeenCalled();
          );

          );





          share|improve this answer














          I'd advise you to move your subscription from the constructor to an ngOnInit. Angular created several lifecycle hooks which get called when a component get's created (ngOnInit) and other when data changes or when it gets destroyed - see Angular lifecycle hooks.



          This way you can test your code by calling the ngOnInit() method.



          In case you cannot change the code you can try creating a component instance and check if your method was called like in pseudo-code below:



          import CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA from '@angular/core';
          import async, ComponentFixture, fakeAsync, TestBed, tick from '@angular/core/testing';
          import of from 'rxjs';
          import LayoutComponent from './layout.component';
          import LayoutService from './layout.service';

          describe('LayoutComponent', () =>
          let component: LayoutComponent;
          let fixture: ComponentFixture<LayoutComponent>;
          let serviceSpy: jasmine.SpyObj<LayoutService>;;

          beforeEach(async(() =>
          const spy = spyOn(service.messagePublished$, 'subscribe')
          TestBed.configureTestingModule(
          declarations: [
          LayoutComponent,

          ],
          providers: [
          provide: LayoutService, useValue: spy
          ],
          schemas: [
          NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA
          ]
          )
          .compileComponents();
          serviceSpy = TestBed.get(ValueService);
          ));

          beforeEach(() =>
          fixture = TestBed.createComponent(LayoutComponent);
          component = fixture.componentInstance;
          fixture.detectChanges();

          component.message = 'Garbage';
          );

          it('should call messagePublished', () =>
          TestBed.createComponent(LayoutComponent);

          expect(service.messagePublished$.subscribe).toHaveBeenCalled();
          );

          );






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 at 10:06

























          answered Nov 12 at 11:44









          Mac_W

          805415




          805415











          • That's a good idea but I'm only in charge of unit testing right now. So I can't really change the code. I have to figure out a way to access the code when it's in the constructor.
            – Simba3696
            Nov 12 at 12:14










          • Updated the code for testing constructor tests. Hopefully it should give you an idea to how to check if the code has been run.
            – Mac_W
            Nov 12 at 12:21










          • It is because you replace your LayoutService with a mock in this line: provide: LayoutService, useValue: mockLayoutService and mockLayoutService doesn't have a property of messagePublished$. mockLayoutService needs to be an object with a property messagePublished$ which has a subscribe property.
            – Mac_W
            Nov 12 at 14:44











          • Do you want to update your code? I'll try and help
            – Mac_W
            Nov 13 at 8:34










          • Code updated. I hope it gives you a clearer picture.
            – Simba3696
            Nov 13 at 9:08
















          • That's a good idea but I'm only in charge of unit testing right now. So I can't really change the code. I have to figure out a way to access the code when it's in the constructor.
            – Simba3696
            Nov 12 at 12:14










          • Updated the code for testing constructor tests. Hopefully it should give you an idea to how to check if the code has been run.
            – Mac_W
            Nov 12 at 12:21










          • It is because you replace your LayoutService with a mock in this line: provide: LayoutService, useValue: mockLayoutService and mockLayoutService doesn't have a property of messagePublished$. mockLayoutService needs to be an object with a property messagePublished$ which has a subscribe property.
            – Mac_W
            Nov 12 at 14:44











          • Do you want to update your code? I'll try and help
            – Mac_W
            Nov 13 at 8:34










          • Code updated. I hope it gives you a clearer picture.
            – Simba3696
            Nov 13 at 9:08















          That's a good idea but I'm only in charge of unit testing right now. So I can't really change the code. I have to figure out a way to access the code when it's in the constructor.
          – Simba3696
          Nov 12 at 12:14




          That's a good idea but I'm only in charge of unit testing right now. So I can't really change the code. I have to figure out a way to access the code when it's in the constructor.
          – Simba3696
          Nov 12 at 12:14












          Updated the code for testing constructor tests. Hopefully it should give you an idea to how to check if the code has been run.
          – Mac_W
          Nov 12 at 12:21




          Updated the code for testing constructor tests. Hopefully it should give you an idea to how to check if the code has been run.
          – Mac_W
          Nov 12 at 12:21












          It is because you replace your LayoutService with a mock in this line: provide: LayoutService, useValue: mockLayoutService and mockLayoutService doesn't have a property of messagePublished$. mockLayoutService needs to be an object with a property messagePublished$ which has a subscribe property.
          – Mac_W
          Nov 12 at 14:44





          It is because you replace your LayoutService with a mock in this line: provide: LayoutService, useValue: mockLayoutService and mockLayoutService doesn't have a property of messagePublished$. mockLayoutService needs to be an object with a property messagePublished$ which has a subscribe property.
          – Mac_W
          Nov 12 at 14:44













          Do you want to update your code? I'll try and help
          – Mac_W
          Nov 13 at 8:34




          Do you want to update your code? I'll try and help
          – Mac_W
          Nov 13 at 8:34












          Code updated. I hope it gives you a clearer picture.
          – Simba3696
          Nov 13 at 9:08




          Code updated. I hope it gives you a clearer picture.
          – Simba3696
          Nov 13 at 9:08

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260326%2fangular-6-unit-testing-a-subscribe-function-in-constructor%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto