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
}