Practical shift-left on testing

Should You Unit-Test in ASP.NET Core?

Image for post
Image for post
What should be the SUT in tests when you do shift-left on testing?

The Startup pattern, larger units, fewer test doubles and a bit of BDD thinking.

Let’s see what happens if we write isolated, in-memory tests but avoid substituting production code with test doubles as much as possible.

Wait, but why?

Most unit tests are not real tests!

Show me the code

The two AddSingleton statements register two facade interfaces representing the external dependencies. The AddControllers statement registers a group of dependencies; this is a commonly used pattern.
The Startup class is referenced in our service entry point with webBuilder.UseStartup<Startup>.
This will replace our two external dependencies from production code with two test doubles in test code.
The Startup class is referenced in the in our test project entry point with new WebApplicationFactory<Startup>.

If you find that you can follow the dogma rule, then you are in a good position for shift-left on testing.


It is an important goal of testing to learn more about the code and how it works.

Rather than having an inside-out or outside-in approach when you do your steps, try to do both.

The Startup

Medium's largest active publication, followed by +775K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store