我想探索我们是否可以通过设置 AutoMoq 创建的所有 Moq-mocks 默认返回 Fixture-created 值作为方法返回值来节省时间。
在进行如下测试时,这将是有益的:
[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();
}