2015-01-11 69 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實際使用)。 –

    相關問題