我想探索一下,通过设置AutoMoq创建的所有Moq模拟在默认情况下都应该返回Fixture创建的值作为方法返回值,我们是否可以节省时间。
当进行如下测试时,这将是有益的:
[TestMethod]
public void Client_Search_SendsRestRequest()
var client = fixture.Create<Client>();
// Could be removed by implementing the mentioned functionality
Mock.Of(JsonGenerator).Setup(j => j.Search(It.IsAny<string>())).Returns(create("JsonBody")));
client.Search(fixture.Create("query"));
Mock.Of(client.RestClient).Verify(c => c.Execute(It.IsAny<RestRequest>()));
Mock.Of(client.RestClient).Verify(c => c.Execute(It.Is<RestRequest>(r => record(r.Body) == record(client.JsonGenerator.Search(query)))));
}
请注意,生成的值必须缓存在(?)对于代理,我们希望相同的值“冻结”以进行检查。此外,使用
Setup
应覆盖创建的值。
那么,我们如何修改AutoMoq模拟来做到这一点呢?
验证其工作的简单测试可以是:
[TestMethod]
public void MockMethodsShouldReturnCreatedValues()
{
Guid.Parse(new Fixture().Create<ITest>().Test());
}
public interface ITest
{
string Test();
}