2013-02-20 56 views
2

我不能測試任何註銷,登錄,從新的Microsoft Fake框架AccountController註冊操作,而沒有此錯誤消息:System.Security.VerificationException:操作可能會破壞運行時。單元測試WebMatrix.WebData與微軟虛假框架MVC4

單元測試是真實簡單:

[TestMethod] 
    public void LogOff() 
    { 
     var AccountController = new AccountController(); 
     RedirectToRouteResult RedirectToRouteResult; 

     //Scope the detours we're creating 
     using (ShimsContext.Create()) 
     { 
      ShimWebSecurity.Logout =() => { }; 
      var test = AccountController.LogOff(); 
      RedirectToRouteResult = AccountController.LogOff() as RedirectToRouteResult; 
     } 

     Assert.IsNotNull(RedirectToRouteResult); 
     Assert.AreEqual("Index", RedirectToRouteResult.RouteValues["Action"]); 
     Assert.AreEqual("Home", RedirectToRouteResult.RouteValues["controller"]); 
    } 

也覺得這樣的:http://social.msdn.microsoft.com/Forums/en-US/vsunittest/thread/f84962ea-a9b2-4e0d-873b-e3cf8cfb37e2是談論同樣的錯誤,但無人接聽。

謝謝!

回答

1

我在VS2012 Update 1發佈之前問過同樣的問題(VerificationException when instantiating MVC controller during unit testing),我收到了一位來自微軟的人的回覆,他說他們正在開發它,並且應該在下一次更新中提供。那麼從那以後什麼都沒有。但是,爲了獲得結果並繼續使用Microsoft Fakes Framework進行測試,我用類似於UrlHelper類的MVC方法調用了我自己的私有方法,該方法返回基本類型,如string,然後將單位測試給我一個理想的結果。這樣我從來沒有打電話給底層的MVC基礎設施,我得到了預期的結果。此外,您將需要刪除System.Web.Mvc.Fakes引用,否則VerificationException將不斷彈出。

如果你覺得這個乏味,那麼你應該切換到更成熟的單元測試框架,如Moq或Rhino。

+0

更新2,同樣的錯誤。微軟破壞我的運行時間 – felickz 2013-03-14 14:23:35

+0

@felickz我剛剛測試更新2,它工作正常。我幾天前安裝了更新2,並認爲我可能會給它一個鏡頭。沒有VerificationExceptions。 – 2013-04-08 17:12:52

+0

瘋了我沒有更新2安裝在我使用的機器上。太多的開發機器:) – felickz 2013-05-22 13:45:22