我想探索我们是否可以通过设置 AutoMoq 创建的所有 Moq-mock 默认返回 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)))));
}
Run Code Online (Sandbox Code Playgroud)
请注意,生成的值必须缓存在 (?) 代理中,我们希望“冻结”相同的值以进行检查。此外,设置模拟Setup应该覆盖创建的值。
那么,我们如何修改 AutoMoq 模拟来做到这一点?
验证它是否有效的简单测试可能是:
[TestMethod]
public void MockMethodsShouldReturnCreatedValues()
{
Guid.Parse(new Fixture().Create<ITest>().Test());
}
public interface ITest
{
string Test();
}
Run Code Online (Sandbox Code Playgroud)