tags:

views:

25

answers:

1

When I use SetupAllProperties on a Mock, it works as expected:

/// <summary>
/// demos SetupAllProprties on an interface.  This seems to work fine.
/// </summary>
[Test]
public void Demo_SetupAllProperties_forAnInterface()
{
    var mock = new Mock<IAddress>();

    mock.SetupAllProperties();
    var stub = mock.Object;
    stub.City = "blahsville";

    var retrievedCity = stub.City;
    Assert.AreEqual("blahsville", retrievedCity);
}

However, when I try it on a class, it fails:

/// <summary>
/// demos SetupAllProprties on a class.  This seems to work fine for mocking interfaces, but not classes.  :(  The Get accessor returns null even after setting a property.
/// </summary>
[Test]
public void Demo_SetupAllProperties_forAClass()
{
    var mock = new Mock<Address>();

    mock.SetupAllProperties();
    var stub = mock.Object;
    stub.City = "blahsville";

    var retrievedCity = stub.City;
    Assert.AreEqual("blahsville", retrievedCity);
}

Did I do something wrong? Am I trying to do something unsupported by moq?

For good measure, here are the IAddress interface and the Address class:

public interface IAddress
{
    string City { get; set; }
    string State { get; set; }
    void SomeMethod(string arg1, string arg2);
    string GetFormattedAddress();
}

public class Address : IAddress
{
    #region IAddress Members
    public virtual string City { get; set; }
    public virtual string State { get; set; }
    public virtual string GetFormattedAddress()
    {
        return City + ", " + State;
    }

    public virtual void SomeMethod(string arg1, string arg2)
    {
        // blah!
    }
    #endregion
}
+1  A: 

I copied your code into a new project could not reproduce your problem. I set a breakpoint in Demo_SetupAllProperties_forAClass() at the Assert.AreEqual line and retrievedCity did have the value "blahsville".

I am using xUnit, but I don't think that would make a difference. What version of Moq are you using? I am using 4.0.10510.6.

adrift
I'm using 3.1.0.0. I'll check the issues list, etc.
apollodude217
It must be a version difference. Switching to 4.1.10827 (the latest available on their download page just now) clears the test. :)
apollodude217