2015-01-11 71 views
2

我有一些VS项目相互依赖的设置。简体中文,可想而知这些项目:在Visual Studio 2013中运行单元测试时运行其他项目

  1. API项目(ASP.NET的WebAPI 2)
  2. DummyBackendServer(与WCF服务的WinForms)
  3. API.Test项目(单元测试项目,应测试API项目的控制器)

通常,会有(例如)一个android客户端,它连接到API服务器(1)并请求一些信息。然后,API服务器将连接到DummyBackendServer(2)以请求这些信息,生成适当的格式并向Android客户端发送答案。

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

那么,有什么办法可以告诉VS开始测试依赖的另一个项目吗?

+1

您的单元测试不应该需要API服务器。任何使用API​​服务器的组件都应该使用存根或模拟进行单元测试。 – Maarten

+0

单元测试不需要API服务器 - 它应该测试它的控制器。因此,我不需要启动API服务器,我需要启动后端服务器,API服务器的控制器连接到后端服务器。我没有选择更改此行为,因为 1.我没有权限编辑Api-Project代码 2.我需要测试整个事情 - 包括API-Server和后端服务器之间的通信。 – Rasioc

+0

如果你连接到一个实际的内存不足数据源,那么你不是单元测试,你是集成测试。你确定这是你想要的吗?你使用实体框架? –

回答

0

分而治之!

如果测试(有些人会说那些不是单元测试,但这不是这个问题的一部分)需要一些服务来启动 - 使之发生!将它们部署到某个开发或暂存环境,那么您只需要配置来自API测试程序集的连接。

我会将解决方案分成两部分,称之为集成测试。如果你想他们蜜蜂单元测试你有你需要从上面的帖子。

0

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

哪一个,你会选择是你的,我个人使用Rhino.Mocks

  1. 创建一个模拟库:
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(); 
    
    +0

    像这样嘲讽是不必要的广泛和脆弱。 Entity-Framework和Effort的组合使得这变得更加轻松(但我不知道实体框架是否被OP实际使用)。 –

    相关问题