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

发布于 2024-10-15 10:28:17 字数 771 浏览 2 评论 0原文

这是我拥有的 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);
    }
}

This is the exact representation of a WCF interface and class that I have.

I would like to know how to write a NUnit test for this... using the interface for mocking.

The main thing is I am NOT looking to add a service reference for the WCF service integration test. But some thing more concentrated for the unit testing part. Specifically to test the ProcessStuff method.

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以歌曲疗慰 2024-10-22 10:28:17

好吧,我可能误解了这个问题,所以请随时澄清。

如果被测试的方法是:

ProcessManager.ProcessStuff(input)

IProcessStuff 接口不相关。您将创建一个新的流程管理器并调用此方法。

// 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);

所以在这种情况下,我看不到你想要模拟什么接口。

Okay, I might have mis-read the question, so feel free to clarify.

If the method under test is:

ProcessManager.ProcessStuff(input)

The IProcessStuff interface is irrelevant. You would create a new Process Manager and call this method.

// 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);

So in this case, I cannot see what Interface you want to mock.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文