-1

Any one can explain how to create Stubs for service mocking for Angular Karma testing. Please provide any complete example or link. After creating the stub, how to write the test cases using stub data. Please explain.

Spy Vs stub which one is best practice. Please suggest. Thanks in advance.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
YYY
  • 3,440
  • 5
  • 20
  • 24

1 Answers1

2

You can find about creating stubs and spies in this article of mine.

Coming to your 2nd question, choosing stub and spy is more about the implementation scenarios. One can used over other based on situation. For ex:

  1. Use Stub to replace actual service. In this way, you can reuse the same stub at other components.spec as well. Hence it creates reusable code.

  2. Now, there are times when your services returns value differently depending on code (lets say, it throws error in some http call and you have handled that in your code). To test such conditions, you can simple create a spy and override the stub response. Check my article where I have done the same.

  3. If its a quick overriding of values, use spy

I hope it helped.

Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104