Mark Oliver's World

Posted: 02/04/2021

Testing This Site

This site started as an easy way to learn Azure Static Web Apps, and an ego thing to figure out if I could build a WASM based blog.

I have satisfied those things for sure, but I have also realised I like writing these posts!

So Im keeping this site around, which means the code I have written needs to be tested.
I abhor code that is not tested if its being kept.

That said, it does not need to be "100% tested" (whatever that is), but the important parts should be tested.

This site has 2 parts to it:

  • A front end set of components written in Blazor.
  • A "back end" set of classes

Each of these require different ways of testing:

  • Front end - This requires testing the component as visual entities. This can be achieved in Blazor using bUnit
  • Back end - This can be tested using unit tests of the classes & methods

You may also want to consider running bigger tests that check that the "site" works in its entirety. These are End To End tests, or integration tests!

To test this way, it would require using something like Selenium. Considering this is my personal blog, Im going to focus on unit testing the back end and the key front end components. I really dont need to put the effort into Integration tests, I wont learn much more about Selenium!

Back end testing

This type of testing is the most familiar to me. I do a lot of this type of testing every day. I also am pushing closer and closer to a TDD style approach!

I took one look at the core of my Blog code, and realised it was not very testable without Mocking, or even making internals public.
This is not good, and one of the biggest benefits of testing your code. If you try and test it, and its hard to do, then your code is more complicated than it needs to be.
Testing as you develop (not after) teaches you so much about design and putting your code together, that I often feel I get more out of it from that perspective than the actual tests.

So the first thing I did was not write any tests, but to refactor the core component of my blog, so it is much more testable.
The involved refactoring 1 class into 4 classes, considering "Single Responsibility Principle" the whole time.
This gives a much easier set of things to test, that require only a single Mock to remove the HttpClient necessity. Now it can be tested!

More to come another day on the Front end testing.


Thanks for reading this post.

If you want to reach out, catch me on Twitter!

I am always open to mentoring people, so get in touch.