Angular2 unit testing : testing a component's constructor

后端 未结 3 1013
盖世英雄少女心
盖世英雄少女心 2021-02-07 12:20

All is in the title : how can one test what is done in the component\'s constructor ?

For your information, I am using a service that requires a setting

3条回答
  •  时光说笑
    2021-02-07 12:51

    Since OP states "I would like to see if the 2 methods that I call in the constructor are called correctly." I have a better approach.

    Write a unit test. You don't need to use the test bed for this. It will slow down your tests a lot. Instantiate your mocks manually. Set your spies on the methods you're interested in and then call the component constructor manually with the stubs you've instantiated and set spies on. Then test if spied methods have been called correctly.

    The key is to extend your stubs from the original service classes. jasmine.createSpyObj helps for mocking angular classes like Router.

提交回复
热议问题