Search This Blog

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);
        }
    }
}