Unit testing controllers

Unit testing controllers

Set up unit tests of controller actions to focus on the controller's behavior. A controller unit test avoids scenarios such as filters, routing, and model binding. Tests that cover the interactions among components that collectively respond to a request are handled by integration tests. For more information on integration tests, see Integration tests in ASP.NET Core.

If you're writing custom filters and routes, unit test them in isolation, not as part of tests on a particular controller action. To demonstrate controller unit tests, review the following controller in the sample app.

Testing built packages locally with moq

You can either build from command line or explicitly Pack (from the context menu) the Moq.Package project.

Packages are generated in the bin folder in the repository root. To test these packages you can just add a package source pointing to it. You can also just place a NuGet.Config like the following anywhere above the directory with the test solution(s):

<configuration>
	<packageSources>
		<add key="moq" value="[cloned repo dir]\bin" />
  </packageSources>
</configuration>

Walkthrough: Create and run unit tests for managed code

using System;

namespace BankAccountNS
{
    /// <summary>
    /// Bank account demo class.
    /// </summary>
    public class BankAccount
    {
        private readonly string m_customerName;
        private double m_balance;

        private BankAccount() { }

        public BankAccount(string customerName, double balance)
        {
            m_customerName = customerName;
            m_balance = balance;
        }

        public string CustomerName
        {
            get { return m_customerName; }
        }

        public double Balance
        {
            get { return m_balance; }
        }

        public void Debit(double amount)
        {
            if (amount > m_balance)
            {
                throw new ArgumentOutOfRangeException("amount");
            }

            if (amount < 0)
            {
                throw new ArgumentOutOfRangeException("amount");
            }

            m_balance += amount; // intentionally incorrect code
        }

        public void Credit(double amount)
        {
            if (amount < 0)
            {
                throw new ArgumentOutOfRangeException("amount");
            }

            m_balance += amount;
        }

        public static void Main()
        {
            BankAccount ba = new BankAccount("Mr. Bryan Walton", 11.99);

            ba.Credit(5.77);
            ba.Debit(11.22);
            Console.WriteLine("Current balance is ${0}", ba.Balance);
        }
    }
}

Create a unit test project

  • On the File menu, select Add > New Project.

  • Type unit test in the search box, select C# as the language, and then select the C# Unit Test Project for .NET Core template, and then click Next.

  • Name the project BankTests and click Next.

  • Choose either the recommended target framework (.NET Core 3.1) or .NET 5, and then choose Create. The BankTests project is added to the Bank solution.

  • In the BankTests project, add a reference to the Bank project. In Solution Explorer, select Dependencies under the BankTests project and then choose Add Reference from the right-click menu.

  • In the Reference Manager dialog box, expand Projects, select Solution, and then check the Bank item.

  • Choose OK.

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BankTests
{
    [TestClass]
    public class BankAccountTests
    {
        [TestMethod]
        public void TestMethod1()
        {
        }
    }
}

To keep reading more about simple test environment in c# please take a look here

Last updated