Verifying a reference property in moq (NUnit)

I started with using moq for unit tests. All I wanna do is this: test "Execute" method of class A. the method accepts an IA type object and sets a simple property in it.

[TestFixture]
public class A
{
    public void Execute(object s)
    {
        if (s is IA)
        {
            (s as IA).ASimpleStringProperty = "MocktestValue";
        }
    }
}

public interface IA
{
    string ASimpleStringProperty { get; set; }
}

I wrote my unit test like this:

but this does not work with my test method below: Any ideas where I am going wrong?

[Test]
public void TestMethod1()
{
    var person = new Mock<IA>();
    var a = new A();
    a.Execute(person.Object);
    person.VerifySet(ASimpleStringProperty = "MockytestValue", "FailedTest");
}

(I want to check if the ASimpleStringProperty is "Mocktestvalue" but couldn't for some reasons. Also, When I put the debugging, i see ASimpleStringProperty is null!

Upvotes: 0

Views: 502

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

You have typo in value which you assigning to property - MockytestValue instead of MocktestValue. Also use VerifySet to check if property was set:

person.VerifySet(ia => ia.ASimpleStringProperty = "MocktestValue", "FailedTest");

BTW why your A class marked as TestFixture?

Upvotes: 2

Related Questions