如何使用模拟界面为此方法编写NUnit测试?

时间:2011-01-31 19:36:00

标签: c# wcf unit-testing nunit mocking

这是我所拥有的WCF接口和类的精确表示。

我想知道如何为此编写NUnit测试......使用模拟界面。

主要的是我不打算为WCF服务集成测试添加服务引用。但有些事情更集中在单元测试部分。专门用于测试ProcessStuff方法。

namespace CompanyName.Services.ProcessStuff {
 public class ProcessStuffService : IProcessStuff
    {

        public string ProcessStuff(string input)
        {
            ISomeOtherInterface ProcessManager = new ProcessManager();
            return ProcessManager.ProcessStuff(input);            
        }

    }
}


namespace CompanyName.Common.FacadeInterfaces
{
    [ServiceContract(Namespace = "com.companyname.services", Name = "Process Stuff Service")]
    public interface IProcessStuff
    {
        [OperationContract(Name = "ProcessStuff")]
        string ProcessStuff(string input);
    }
}

1 个答案:

答案 0 :(得分:1)

好的,我可能会误读这个问题,所以请随时澄清。

如果测试方法是:

ProcessManager.ProcessStuff(input)

IProcessStuff接口无关紧要。您将创建一个新的Process Manager并调用此方法。

// You would pass mocks IN to the process manager if it required them here...
var processManager = new ProcessManager();
var result = processManager.ProcessStuff(testInput);
Assert.IsNotNull(result);

所以在这种情况下,我无法看到你想要模拟的界面。