moq - 模拟 <SomeClassType>. SetupAllProperties() 会导致属性返回它们指定的值?

  显示原文与译文双语对照的内容
0 0

当我在Mock上使用SetupAllProperties时,它按预期工作:


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

但是,当我在一个类上尝试它时,它失败了:


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

我 是不是 做错 什么 了? 我是在做一些不支持的东西?

对于好的度量,下面是IAddress接口和地址类:


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
}

时间: 原作者:

0 0

我把你的代码复制到一个新的项目中,NOT 可以重现你的问题。 我设置了一个断点 Demo_SetupAllProperties_forAClass()Assert.AreEqual 行和 retrievedCity 中,有一个值 "blahsville"

我使用的是xUnit但是我想 NOT 会造成。 你用的是什么版本的Moq? 我正在使用 4.0.105 10.6.

原作者:
...