2012-07-24 73 views
5

我只是試圖讓熟悉的Visual Studio 2012 RC的新正版正貨分離框架,但我因此與ShimNotSupportedException小號面臨的問題。
在第一次嘗試,我試圖掛鉤委託各單墊片的方法,已經嘗試運行/調試測試時,拋出一個ShimNotSupportedExceptionShimNotSupportedException在MS的VisualStudio 2012

[TestMethod] 
public void GetFoo_ValidBar_ReturnsBaz() 
{ 
    using(ShimsContext.Create()) 
    { 
     ShimDateTime.NowGet =() => new DateTime(2012,08,11,10,20,59); 

     const string expected = "20120811_102059"; 
     string actual = GetFoo(); 

     Assert.AreEqual(expected,actual); 
    } 
} 

這是相應的堆棧跟蹤:

的 GetFoo_ValidBar_ReturnsBaz 測試方法已拋出異常: Microsoft.QualityTools.Testing.Fakes.Shims.ShimNotSupportedException: 的System.DateTime 在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.InvokeEvent(T值,措施1 EH) 在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.OnAttachedUnsupported方法(MethodBase方法) 在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.CheckInstrumentation(MethodBase 方法) 在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.InternalAttachDetour(對象 optionalReceiver,MethodBase方法,委託detourDelegate ) 在Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationRuntime.AttachDetour(對象 optionalReceiver,MethodBase方法,委託detourDelegate) 在Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.SetShimMethod(代表 optionalStub,對象optionalReceiver, MethodBase方法) 在Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.SetShim(代表 optionalStub,類型receiverType,對象optionalReceiver,字符串名稱, ShimBinding標誌類型返回類型,類型[] parameterTypes) 在Microsoft.QualityTools.Testing.Fakes.Shims.ShimRuntime.SetShimPublicStatic(代表 optionalStub,類型receiverType,字符串名稱,鍵入返回類型,類型[] parameterTypes) 在System.Fakes.ShimDateTime.set_NowGet(Func'1值) 在GetFoo_ValidBar_ReturnsBaz() 在BazTests.cs:管線48

閱讀了兩個線程之後我發現在MSDN處理這個問題,我遵循他們的指示(關閉CodeCoverage,刪除.testsettings f ile)這不適合我!
儘管如此,我已經找到了解決此問題的方法:
通過首先運行測試瀏覽器中的所有測試(而不是直接從編碼區域使用「MSTest測試(單擊運行)」按鈕),所有工作都正確無誤異常被拋出。之後,我甚至可以調試測試,並按照預期的方式分配給填充方法。
這適用於我以後使用的所有以下墊片。
但現在我想實現對數據庫訪問的MS企業庫的假貨當再次有同樣的問題。

這是測試的樣子:

[TestMethod] 
public void GetFooFromEF_NonEmptyDataReader_ObjectsCorrectlyInstantiated() 
{ 
    using(ShimsContext.Create()){ 
     var dataReader = new StubIDataReader() 
      { 
       ItemGetString = s => 1, 
       DepthGet =() => 2 
      }; 

     ShimFoo.GetBar = guid => dataReader; 

     var bar = new StubIBar() 
     { 
      ConvertIBarToBaz = record => null 
     }; 

     ShimQux.AllInstances.GetBar = (a, b) => bar; 

     var dbFactory = new StubDbProviderFactory(); 
     var db = new StubDatabase("test", dbFactory); 
     ShimDatabaseFactory.CreateDatabaseString = s => db; 

     List<BarInformation> actual = accessor.InvokeStatic("GetBar", 
                   new Object[] { }) as List<BarInformation>; 
     Assert.IsTrue(true); 
    } 
} 

前兩個墊片分配(ShimFoo & ShimQux)是否按預期工作。但ShimDatabaseFactory.CreateDatabaseString(這應該是DatabaseFactory。CreateDatabase(string)在嘗試創建新數據庫實例時返回存根數據庫)會再次拋出ShimNotSupportedException。我無法弄清楚爲什麼!
你有什麼想法在這裏出了什麼問題?

我將不勝感激任何輸入。

感謝,
本傑明

+0

只是一個警告,你需要運行VS作爲管理員地段後,終於達成。 – 2012-12-07 19:35:41

回答

4

我有同樣的確切問題。嘗試從磁盤和解決方案中刪除所有測試設置文件,並確保您的解決方案不參考任何測試設置文件。

另外,請確保您使用的是visual studio testrunner(而不是resharper等,它正在測試代碼)。

我寫這些問題可能會有所幫助2點的相關博客文章:

Visual Studio 2012 Fakes – ShimNotSupportedException when debugging tests

Unit testing – Visual Studio 2012 Fakes in Team City

+0

ReSharper是我的問題。 – MasterMastic 2013-05-05 14:37:09

1

我已經看到了這個錯誤有不同的原因多次:

  • 您的假貨生成文件存在錯誤或問題,其中一些文件生成不正確。清理目錄並重制你的假引用
  • 依賴的dll丟失。在這種情況下,你錯過了一個虛假的dll依賴的DLL。在一個案例中,我正在填充一個webservice並且缺少System.ServiceModel dll。
  • 有時您可以通過更改測試設置默認處理器架構來修復它。但是我不知道爲什麼,它可能會刷新一些緩存的dll。