Search This Blog

Showing posts with label Unit tests. Show all posts
Showing posts with label Unit tests. Show all posts

Tuesday, January 5, 2016

C# Unit test to check if the class implements an interface with IsInstanceOfType method

C# > Assert Class > IsInstanceOfType

This method verifies that the specified object is an instance of the specified type. 

Example

Test class implements a specific interface.

var result = typeof(MyClass).GetInterfaces()[0];

// Assert

Assert.IsInstanceOfType(typeof(IMyClass), result.GetType());







Monday, May 18, 2015

C# unit test using Moq verify property has been set

C# > Unit test > Moq > Verify property has been set

SetupSet : Specifies a setup on the mocked type for a call to a property setter.
VerifySet : Verifies that a property has been set on the mock, regardless of its value.

Example

using System;

namespace UnitTestProject1
{
    public interface Interface1
    {
        int Id { get; set; }
    }
}

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var mocker = new Mock<Interface1>();
            mocker.SetupSet(x => x.Id = 1);
            mocker.Object.Id = 1;
            mocker.VerifySet(x => x.Id);
        }
    }
}