Whether you’re new to software development or have been around for awhile, you may have heard of TDD, but have never really gotten a full explanation of the process and benefits of adoption. The purpose of this post is to give you a quick introduction to TDD and hopefully pique your interest if you don’t already have TDD in your arsenal of tools.
So what is TDD? To put it simply,
TDD (Test Driven Development) is a methodology of developing software
that relies on very short feedback loops using unit tests.
To practice TDD, start with a failing unit test. Then write only enough code to make that test pass. Finally, refactor the production and unit test code you’ve written. Repeat. This cycle is often called Red Green Refactor cycle. …
In my previous post, Angular Component Class Tests, I shared why I prefer to write my Angular unit tests exclusively as component class tests. In this post, I want to share a simple function I use to create typed stubs.
In my previous post, you’ll notice I had to cast my dependencies to the Typescript
any type in order to inject them into the constructor. Here’s that example again:
Now, I could have easily used the real type of my service dependency and simply stubbed all of its methods. That is easy enough if your dependency only has a few properties. However, if you’re injecting something like Angular’s
When testing Angular components, I almost exclusively use component class tests. These are tests that do not use the TestBed and allow you to test a component the same way you would test a service.
The main reason I prefer component class tests is that they are much easier to write than using the TestBed. …