MVVM Unit Testing in .Net Maui Windows Application

Ronald Rex 1,666 Reputation points
2024-01-12T14:40:47.45+00:00

I am trying to do some MVVM unit testing using xUnit. My ViewModel has a dependency and i was wondering as I try to test it how would I handle this dependency. Thanks !

public partial class MainPageViewModel : ObservableObject
{
    private readonly IWrkDirQueries _wrkData;
    public MainPageViewModel(IWrkDirQueries wrkDirDataAccess)
    {
        _wrkData = wrkDirDataAccess;
        RefreshCustomerList();
    }
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,903 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,283 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 48,181 Reputation points Microsoft Vendor
    2024-01-15T03:05:55.8533333+00:00

    Hello,

    You can test this ViewModel using dependency injection or instantiation of this parameter.

    Please refer to Testing MVVM applications for officially recommended best practices.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.