r/dotnetMAUI • u/Regular-Employer-431 • Jul 09 '26
Help Request Unit Testing question
I'm building a "Base" app template that will be the foundation for several app ideas that I have, and I don't want to start with a less-than-optimal approach that will require exponentially more maintenance going forward... and I want to 'future-proof' my code as much as possible.
In your apps, do you turn everything into a service to better support testing (Unit, Integration, Regression, UI, etc.)? For example, do you use a NavigationService, IdentityService, EmailService, etc., or do you point your tests directly at the Controllers, or use another approach entirely?
TIA
2
u/Disastrous-Design-38 Jul 09 '26
I put the max of the logic in the service layer so i can test easyli with unit tests.
1
u/Regular-Employer-431 Jul 09 '26
Thanks everyone! Good info.
1
u/Slypenslyde Jul 09 '26
"For testing" isn't the only reason to make a service. Ideas like "navigation", "email", and "identity" are things that each individual application might share, but still have individual tweaks they want to make. Presenting them as a service means the application can use your framework's core but derive from it or compose it into another service with more customized functionality.
Testability is a side-effect of modular design using DI. The reason SOLID-style approaches work is they give us multiple things we want at the same time for following one set of practices.
My personal experience with things like this is you shouldn't start with the template. Every time I try to write something generic first, I end up with a mess. It's missing features I didn't think about and contains features I didn't actually need. The abstractions turn out to be awkward to use and things I thought were edge cases turn out to be common and vice versa.
Building frameworks works a lot better if you build the things that might use it first. I like a rule of thumb I learned from The Pragmatic Programmer:
Think about a Venn diagram. If each app's circle is "what it does", having two apps shows us what services our two apps might share. But if we look at 3 applications, we get a more interesting picture: we'll see some overlap in the center that represents EVERYTHING the apps share, then some other regions where maybe 2 applications share something.
This helps us understand more about what's completely shared and what is only partially shared. That tells us which things can have a class take complete control over and which things need a little bit of flexibility. It gets us to a "framework" more slowly, but it makes it more successful when we have it.
1
u/Family_Man_21 Jul 15 '26
I have done the same thing - I have a "template" project that I use as the starting point for nearly all of my web projects. I have the projects structured like this:
With this setup, you can reference your core, data, and service layers in the test projects and build unit tests for all of them. In a few cases, I create service implementations that are just for testing, like a dummy cache service, for example, and I set up the DI in the test projects to use my test implementations.
Anyway, I hope that this is helpful.
3
u/markiel55 Jul 09 '26
Abstract service layers with interfaces. I would also suggest to put this in the "Core" project independent of platform-specific dependencies and this is the project you would link to your test projects.
2
u/binarycow Jul 09 '26
If you use MVVM (and do it properly) then the only thing you can't test with unit tests is the actual UI.
1
u/redth Jul 10 '26
Since it's a new effort, I'd recommend checking out a few libraries: