dotnet-architecture / eshop-mobile-client

eShop mobile & desktop client built with .NET MAUI

Home Page:https://dotnet.microsoft.com/learn/maui/architecture

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do you test services?

mdelgadov opened this issue · comments

I'm trying to do TDD but I'm finding it hard to deal with Preferences and Shell.Current objects that can't be injected and are created by Maui at running time.
I reviewed your brilliant application and found that you access all of them through services, freeing the View Models of references.

But, isn't it just passing the buck? At the end, the services aren't tested. I can test MVVM fine, but not Services.

Is that the case? You don't have a way to test your services in your code?

To illustrate:
This comes from the Settings Service:

public string AuthAccessToken
{
get => Preferences.Get(AccessToken, AccessTokenDefault);
set => Preferences.Set(AccessToken, value);
}

Now, I don't know a way to mock or stub the Preferences static class in the last two lines. If I want to test the service, I can't, because the Preferences static object is not injected but created behind the scenes.