【发布时间】:2014-11-26 11:29:59
【问题描述】:
目前我面临对生产代码进行单元测试的挑战。我们有一个函数可以从传入的 WCF 消息中检索 IP 地址。
public void DoSomething(){
var ipAddressFromMessage = GetIpFromWcfMessage();
var IpAddress = IPAddress.Parse(ipAddressFromMessage);
if(IpAddress.IsLoopback)
{
// do something
}
else
{
// do something else
}
}
private string GetIpFromWcfMessage()
{
OperationContext context = OperationContext.Current;
string ip = ...//use the IP from context.IncomingMessageProperties to extract the ip
return ip;
}
问题是,我应该怎么做才能测试DoSomething()中的IP检查?
[Test]
Public void DoSomethingTest()
{
//Arrange...
// Mock OperationContext so that we can manipulate the ip address in the message
// Assert.
...
}
我是否应该改变我使用 Operation 上下文的方式,以便我可以模拟它(例如,实现一个接口并模拟该接口的实现)?
【问题讨论】:
标签: c# wcf unit-testing mocking rhino-mocks