Category Archives: Nunit

Example test your controller, Nunit test

for you home controller:

using System.Web.Mvc;

namespace WestBanking.Controllers
{
    public class HomeController : Controller
    {
       

        public ActionResult Index()
        {
            return Redirect("http://netbank.test.com");
        }
    }
}

here is your test

using WestBanking.Controllers;
using NUnit.Framework;
using TestStack.FluentMVCTesting;

namespace WestBanking.ControllerTests   
{
    [TestFixture]
    public class HomeControllerTests
    {
        [Test]
        public void ShouldRedirect()
        {
            var sut = new HomeController();

            sut.WithCallTo(x => x.Index()).ShouldRedirectTo("http://netbank.test.com");
        }
    }
}

Example of using moq in your nunit test

When you do unit test, we won’t use the real server to do the test, the moq package could help us do the dependence inject.

here is the sample example how to use moq to mock your inject classes

using WestBanking.Models;
using Moq;
using NUnit.Framework;

namespace BankingSite.UnitTests
{
    [TestFixture]
    public class LoanApplicationScorerTests
    {
        [Test]
        public void ShouldDecline_TooYoung()
        {
            var fakeCreditChecker = new Mock();            
            
            //go pass the CheckCredit method
            fakeCreditChecker.Setup(
                x => x.CheckCredit(It.IsAny<int>())
                .Returns(true);
            //inject the fake instance here
            var sut = new LoanApplicationScorer(fakeCreditChecker.Object);

            var application = new LoanApplication
                              {
                                  Age = 21
                              };

            sut.ScoreApplication(application);

            Assert.That(application.IsAccepted, Is.False);
        }


        [Test]
        public void ShouldOK_Young_Wealthy()
        {
            var fakeCreditChecker = new Mock();

            fakeCreditChecker.Setup(
                x => x.CheckCreditHistory(It.IsAny<int>())
                .Returns(true);

            var sut = new LoanApplicationScorer(fakeCreditChecker.Object);

            var application = new LoanApplication
            {
                AnnualIncome = 1500000.01m,
                Age = 21
            };

            sut.ScoreApplication(application);

            Assert.That(application.IsAccepted, Is.True);
        }


        [Test]
        public void ShouldDecline_NotYoung_PoorCredit()
        {
             //create a instance of fake class
            var fakeCreditHistoryChecker = new Mock();

            fakeCreditChecker.Setup(
                x => x.CheckCreditHistory(It.IsAny<int>()))
                .Returns(false);

            var sut = new LoanApplicationScorer(fakeCreditHistoryChecker.Object);

            var application = new LoanApplication
            {
                AnnualIncome = 30000.01m,
                Age = 30
            };

            sut.ScoreApplication(application);

            Assert.That(application.IsAccepted, Is.False);
        }


    }
}

Write your first Unit test – Test your model

For example you have very simple code like this:

namespace WestBanking.Models
{
    public class CheckCredit: ICreditChecker
    {
        public bool IsCreditWorthy(int credit)
        {
            // Simulate actual credit check

            if (credit<200)
            {
                return false;
            }

            return true;
        }
    }
}

For the test code:

using WestBank.Models;
using NUnit.Framework;

namespace BankingSite.UnitTests
{
    [TestFixture]
    public class CreditCheckerTests
    {
        [Test]
        public void BadCredit()
        {
            //sut: system under test
            var sut = new CreditChecker();

            var isCreditWorthy = sut.IsCreditWorthy(100);

            Assert.That(isCreditWorthy, Is.False);
        }

        [Test]
        public void GoodCredit()
        {
            var sut = new CreditChecker();

            var isCreditWorthy = sut.IsCreditWorthy(1000);

            Assert.That(isCreditWorthy, Is.True);
        }
    }
}

Nunit test in Visio Studio – Set up – To be continue

Recently my job need write all the unit test for my project and I haven’t done this before, and I like share my experience here, and hope that could help someone else.

First, Install an test runner  that will help you do the test in Visual Studio.

Tool > extension and updates > search online> nunit test adapter

nunit test

Second, create an test project

Third, Open the PM console, install some useful packages:

Nunit

Moq

TestStack.FluentMVCTesting