.NET MAUI – Forget Me Not – 7 – Unit Testing

Picking up where we left off, I want to add unit tests to my program. Now, I know, I should have been using unit tests all along. I have no excuse and hang my head in shame.

To get started, I’ve decided to add xUnit through NuGet. Easy peasy. But it won’t work. The full explanation is here. Great article. Here, in a nutshell are the steps you need to perform:

  1. Create an xUnit test project in the same solution as ForgetMeNot
  2. Make sure it runs
  3. Add a reference to the ForgetMeNot project
  4. This wil not run yet. Here is where the fun begins
  5. First, open up the ForgetMeNot project and change the TargetFrameworks to
<TargetFrameworks>net6.0;net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks>

6. Replace the output statement with

<OutputType Condition="'$(TargetFramework)' != 'net6.0'">Exe</OutputType>

7. Unload ForgetMeNot, reload it, close Visual Studio, open Visual Studio and voila! it will work

(The whys and wherefores are explained very well in the article referenced above)

NSubstitute for mocking

We are now almost ready to begin. I want to use mocking from the very beginning. Until about a weak ago, I’d have installed Moq, but I’m intrigued by NSubstitute, so I’m going to use that.

After assuring myself that the default xUnit test works, I’ll add a file called BuddyTests. In there I will add my first test: ShouldPopulateBuddies. I want to make sure that calling Init() will populate the list of buddies (actually, it could return no buddies, but right now I just want to get this test working).

 [Fact]
 public async Task ShouldPopulateBuddies()
 {
     var buddies = new List<Buddy>();
     var buddy = new Buddy();
     buddies.Add(buddy);
     var buddyService = Substitute.For<IBuddyService>();
     buddyService.GetBuddies().Returns(buddies);
     var buddyListVM = new BuddyListViewModel(buddyService);
     await buddyListVM.Init();
     Assert.True( buddyListVM.Buddies?.Count > 0);

 }

So what is going on here? First I create an empty list of Buddy objects and I create an instance of Buddy which I add to that list.

Next (and here is the cool part) I create a mock for my service with that one line

var buddyService = Substitute.For<IBuddyService>();

Remember that Substitute wants interfaces, so earlier I went to each class in my services, put the cursor on the class name and used Refactor->Extract->ExtractInterface which brought up this wonderful dialog box:

You are using Resharper, right??

I clicked on Select Public to get the public methods and Its own file to put the new interface in a file named IBuddyService.cs. R# is even nice enough to tack the interface name onto the class declaration.

OK, enough digression. My next step is to tell the mock that when the BuddyService’s GetBuddies method is returned, it should return the list of Buddies we just created.

We are now ready to create an instance of the ViewModel and call init() on it. It will use the service we passed in (the mock) and will thus populate its list of buddies with our dummy list and the Assert will pass.

Cool, eh?

This also tells me that I need to handle the case when the list is empty. That means I need to look long and hard at Init() and possibly add another test to prove that the corner case does not cause a problem.

Wrapping Up

It turns out that simple unit tests with mocking is not all that hard to do; and nSubstitute has a lot less ceremony than Moq. I’m going to stay with it for a while and see if I run off a cliff.

Since all the source code changes are already in this blog post, I won’t update the online source until I’ve written some more tests.

Shout-out to Daniel Hindrikes for helping me get started on unit testing for MAUI

See next posting for more unit testing using Fluent asserts.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in .NET MAUI, Essentials, Testing and tagged . Bookmark the permalink.

13 Responses to .NET MAUI – Forget Me Not – 7 – Unit Testing

  1. MichaelVilla says:

    Were you aware that nearly 50% of medication users experience serious drug mistakes because of lack of knowledge?

    Your physical condition requires constant attention. All treatment options you implement significantly affects your body’s functionality. Staying educated about your prescriptions is absolutely essential for successful recovery.
    Your health isn’t just about following prescriptions. Each drug changes your biological systems in potentially dangerous ways.

    Consider these critical facts:
    1. Mixing certain drugs can cause dangerous side effects
    2. Seemingly harmless supplements have strict usage limits
    3. Altering dosages undermines therapy

    To avoid risks, always:
    ✓ Check compatibility with professional help
    ✓ Review guidelines thoroughly when starting new prescriptions
    ✓ Ask your pharmacist about correct dosage

    ___________________________________
    For professional pharmaceutical advice, visit:
    https://members2.boardhost.com/businessbooks6/msg/1729664026.html

  2. I’ve been surfing on-line greater than three hours as of late, but I by no means found any interesting article like yours. It¡¦s pretty worth enough for me. In my opinion, if all website owners and bloggers made good content as you probably did, the internet can be much more useful than ever before.

  3. Dustinser says:

    Traditional timepieces will consistently be fashionable.
    They represent heritage and deliver a mechanical beauty that tech-based options simply don’t replicate.
    Each piece is powered by complex gears, making it both functional and elegant.
    Aficionados value the craft behind them.
    https://whatcar.vn/read-blog/6761
    Wearing a mechanical watch is not just about utility, but about celebrating tradition.
    Their styles are classic, often passed from father to son.
    All in all, mechanical watches will never go out of style.

  4. casino says:

    This website, you can access a wide selection of casino slots from top providers.
    Visitors can enjoy classic slots as well as modern video slots with vivid animation and exciting features.
    If you’re just starting out or a casino enthusiast, there’s a game that fits your style.
    casino slots
    All slot machines are ready to play round the clock and compatible with laptops and mobile devices alike.
    No download is required, so you can get started without hassle.
    Platform layout is intuitive, making it quick to explore new games.
    Join the fun, and enjoy the world of online slots!

  5. play casino says:

    This website, you can find a wide selection of casino slots from famous studios.
    Players can try out classic slots as well as modern video slots with stunning graphics and bonus rounds.
    Even if you’re new or a casino enthusiast, there’s something for everyone.
    slot casino
    All slot machines are available 24/7 and compatible with desktop computers and mobile devices alike.
    No download is required, so you can jump into the action right away.
    Platform layout is user-friendly, making it quick to explore new games.
    Register now, and discover the excitement of spinning reels!

  6. Hairstyles says:

    One thing I would like to say is the fact before obtaining more personal computer memory, look into the machine in which it could well be installed. In case the machine will be running Windows XP, for instance, the actual memory ceiling is 3.25GB. Installing a lot more than this would merely constitute some sort of waste. Make sure one’s mother board can handle the upgrade volume, as well. Good blog post.

  7. Travel says:

    This is very interesting, You’re a very skilled blogger. I’ve joined your rss feed and look forward to seeking more of your magnificent post. Also, I have shared your website in my social networks!

  8. I appreciate, cause I found exactly what I was looking for. You’ve ended my 4 day long hunt! God Bless you man. Have a nice day. Bye

  9. My brother recommended I would possibly like this website. He was once entirely right. This publish actually made my day. You can not consider just how so much time I had spent for this info! Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *