使用 Moq,是否可以设置一个模拟属性以使其发生变异?
例如,给定:
public interface IService // To be mocked.
{
bool IsConnected { get; }
}
是否可以设置一个模拟 IService 以便以下通过?
var mockService = new Mock<IService>();
// Setup mockService so IsConnected mutates...?
bool before = mockService.Object.IsConnected;
bool after = mockService.Object.IsConnected;
Assert.AreNotEqual(before, after);
当然这个设置不能解决问题:
mockService.SetupGet(service => service.IsConnected).Returns(false);
mockService.SetupGet(service => service.IsConnected).Returns(true);
这与我对 Moq 没有实现记录/播放模型的理解一致。
这是一个更现实的例子:
public interface IService // To be mocked.
{
bool IsConnected { get; }
void Connect(); // Mutates IsConnected.
}
class Client // To be tested
{
private readonly IService service;
public Client(IService service)
{
this.service = service;
}
public bool DoWork()
{
if (!service.IsConnected) // In test, IsConnected should return false.
{
service.Connect();
}
if (!service.IsConnected) // In test, IsConnected should return true.
{
return false;
}
// ... yada yada yada more code
return true;
}
}
我正在使用最小起订量 4.0.10827.0。