Search code examples
c#wpf.net-corexunit

How to reference to the project in WPF app for unit testing


I am using Win10, VS Community 2019, latest .NET Core WPF app. I have a very simple WPF app. I would like to test it's ViewModel. I kept MVVM model and keep view and ViewModel separated. Now I would like to unit test the ViewModel. But I can't refer to my project's ViewModel. I have a simple code for as follows:-

namespace Multiples.ViewModel
{
class MultiplesViewModel
{
    public MultiplesViewModel()
    {

    }

    public long GetMultiples(long input)
    {
        long total = 0;
        for (long i = 1; i < input; ++i)
        {
            if (i % 3 == 0 || i % 5 == 0)
            {
                total += i;
            }
        };
        return total;
    }
  }
 }

Later I add a reference to the unit test project. As you can see in the following pic. I can't reference MultiplesViewModel.

enter image description here

So how do I add to MultiplesViewModel for the unit testing?


Solution

  • You must set class MultiplesViewModel as public, otherwise you cannot use it in your unit test project.