Arn*_*kas 27 c# testing moq mocking
我需要以HttpResponseBase.ApplyAppPathModifier
这样的方式进行模拟,即ApplyAppPathModifier
模拟自动返回调用参数.
我有以下代码:
var httpResponseBase = new Mock<HttpResponseBase>();
httpResponseBase.Setup(hrb => hrb.ApplyAppPathModifier(/*capture this param*/))
.Returns(/*return it here*/);
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
编辑:
在Moq文档的第一页(http://code.google.com/p/moq/wiki/QuickStart)上找到了解决方案:
var httpResponseBase = new Mock<HttpResponseBase>();
httpResponseBase.Setup(hrb => hrb.ApplyAppPathModifier(It.IsAny<string>)
.Returns((string value) => value);
Run Code Online (Sandbox Code Playgroud)
我突然觉得很愚蠢,但我想这就是你在23:30写代码时会发生什么
Rus*_*Cam 29
是的,您可以回显传递给该方法的参数
httpResponseBase.Setup(x => x.ApplyAppPathModifier(It.IsAny<string>()))
.Returns((string path) => path);
Run Code Online (Sandbox Code Playgroud)
如果需要,您也可以捕获它
string capturedModifier = null;
httpResponseBase.Setup(x => x.ApplyAppPathModifier(It.IsAny<string>()))
.Callback((string path) => capturedModifier = path);
Run Code Online (Sandbox Code Playgroud)
Ali*_*tad 10
用途It
:
It.Is<MyClass>(mc=>mc == myValue)
Run Code Online (Sandbox Code Playgroud)
在这里,您可以检查期望:您希望收到的价值.在回报方面,只需返回您需要的价值.
var tempS = string.Empty;
var httpResponseBase = new Mock<HttpResponseBase>();
httpResponseBase.Setup(hrb => hrb.ApplyAppPathModifier(It.Is<String>(s=>{
tempS = s;
return s == "value I expect";
})))
.Returns(tempS);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8801 次 |
最近记录: |