Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Visual Studio 2013中运行单元测试时运行其他项目_C#_Wcf_Unit Testing_Visual Studio 2013 - Fatal编程技术网

C# 在Visual Studio 2013中运行单元测试时运行其他项目

C# 在Visual Studio 2013中运行单元测试时运行其他项目,c#,wcf,unit-testing,visual-studio-2013,C#,Wcf,Unit Testing,Visual Studio 2013,我有一些相互依赖的VS项目的设置。简单地说,想象一下这些项目: API项目(ASP.NET WebAPI 2) DummyBackendServer(带有WCF服务的WinForms) 测试项目(应该测试API项目控制器的单元测试项目) 通常,会有(例如)一个android客户端,它连接到API服务器(1)并请求一些信息。然后,API服务器将连接到DummyBackendServer(2)以请求这些信息,生成适当的格式并向android客户端发送答案 现在我需要为API服务器创建一些单元测试。我

我有一些相互依赖的VS项目的设置。简单地说,想象一下这些项目:

  • API项目(ASP.NET WebAPI 2)
  • DummyBackendServer(带有WCF服务的WinForms)
  • 测试项目(应该测试API项目控制器的单元测试项目)
  • 通常,会有(例如)一个android客户端,它连接到API服务器(1)并请求一些信息。然后,API服务器将连接到DummyBackendServer(2)以请求这些信息,生成适当的格式并向android客户端发送答案

    现在我需要为API服务器创建一些单元测试。我的问题是,当运行单元测试时,我没有找到一种方法告诉VS启动DummyBackendServer(2)。当我第一次启动DummyServer时,我无法运行测试,因为菜单选项是灰色的


    那么,有没有办法告诉VS启动测试所依赖的另一个项目?

    您应该在项目中使用IoC容器或类似的东西,这样您就可以在运行单元测试时获得其他项目的
    mock

    您将选择哪一个取决于您,我个人使用:

  • 创建模拟存储库:
  • 将模拟对象添加到存储库:
  • 将模拟对象设置为“Replay”状态,在该状态下,它将重播刚刚录制的操作
  • 调用使用模拟对象的代码
  • 检查是否对模拟对象进行了所有调用
  • 分而治之

    如果测试(有些人会说这些不是单元测试,但这不是问题的一部分)需要一些服务,那就让它发生吧!将它们部署到某个开发人员或登台环境中,那么您只需要配置来自API测试程序集的连接


    我会将解决方案一分为二,称之为集成测试。如果你想让他们做单元测试,你可以从上面的文章中得到你需要的。

    你的单元测试不需要API服务器。任何使用API服务器的组件都应该使用存根或模拟进行单元测试。单元测试不需要API服务器,它应该测试它的控制器。因此,我不需要启动API服务器,我需要启动后端服务器,API服务器的控制器连接到后端服务器。我没有选择改变这种行为,因为1。我没有编辑Api项目代码2的权限。我需要测试整个过程-包括API服务器和后端服务器之间的通信。如果您连接到实际内存不足的数据源,那么您不是单元测试,而是集成测试。你确定这就是你想要的吗?你使用实体框架吗?@杰罗恩瓦内维尔:老实说:你是对的,我没想到……好像我得到了矛盾的指示。。。但是由于单元测试是实际的目标,API服务器需要对此进行一些重大更改。。。我将对此进行研究,并可能会对该任务提出一些更改。-谢谢@拉西奥:我最近碰巧做了一件和你非常相似的事情:安卓、Windows Phone和WebApp前端带有ASP.NET Web API 2后端。您也在使用实体框架吗?如果是这样,我想分享一下我处理单元测试API的方式。现在我有275个功能测试(又称:向API发送请求,API做数据库工作,API响应),运行时间不到10秒。花了一点时间才弄明白所有的事情,但现在API已经在内存中进行了完美的测试,包括数据库操作。如果你也使用EF,那么我的解决方案可能会让你感兴趣。像这样的嘲弄是不必要的广泛和脆弱。实体框架和努力的结合使这变得轻而易举(但我不知道实体框架是否被OP实际使用)。
    MockRepository mocks = new MockRepository();
    
     ISomeInterface robot = (ISomeInterface)mocks.CreateMock(typeof(ISomeInterface));  
     //If you're using C# 2.0, you may use the generic version and avoid upcasting:
    
     ISomeInterface robot = mocks.CreateMock<ISomeInterface>();
    
    // this method has a return type, so wrap it with Expect.Call
    Expect.Call(robot.SendCommand("Wake Up")).Return("Groan");
    
    // this method has void return type, so simply call it
    robot.Poke();
    
    //Note that the parameter values provided in these calls represent those values we  
    //expect our mock to be called with. Similary, the return value represents the value  
    //that the mock will return when this method is called.
    
    //You may expect a method to be called multiple times:
    
    // again, methods that return values use Expect.Call
    Expect.Call(robot.SendCommand("Wake Up")).Return("Groan").Repeat.Twice();
    
    // when no return type, any extra information about the method call
    // is provided immediately after via static methods on LastCall
    robot.Poke();
    LastCall.On(robot).Repeat.Twice();
    
    mocks.ReplayAll();
    
    theButler.GetRobotReady();
    
    mocks.VerifyAll();