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();
    }
Developer technologies | .NET | .NET MAUI
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    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.