2010-08-20 131 views
0

我使用起訂量的框架工作的一個單元測試項目中,C#4.0中,當無法加載文件或程序MVC2.0C#單元測試錯誤,在運行測試

測試類似於一個在下面。但是,只要我運行這個測試,我得到一個奇怪的錯誤。我已經檢查了所有引用的System.Web.mvc程序集,並且它們都在2.0.0版本中,所以在我看來這些程序不會導致問題。

[TestMethod] 
     public void PaymentStepOne_Should_Return_RedirectUrl_Because_CustomerId_In_Session_Is_Null() 
     { 
      var _mockFrontendWebshopController2 = new Mock<FrontendWebshopController> 
      (                    
       _mockCartItemService.Object, _mockCartService.Object,                           
       _mockOrderService.Object, _mockCustomerService.Object,                    
       _mockVariantService.Object, _mockShippingCostService.Object,                    
       _mockNodeService.Object, _mockPageSettingService.Object, 
       _mockUserProfileService.Object) { CallBase = true }; 

      var config = ConfigurationManagerHelper.GetConfigurationManager(new DefaultSettings()); 
      _mockFrontendWebshopController2.Setup(x => x.GetConfigurationManager()).Returns(config); 

      var webshopController = _mockFrontendWebshopController2.Object; 


      webshopController.SetFakeControllerContext(); 
      webshopController.Response.Redirect("http://www.google.nl"); 

      var idToUse = Guid.NewGuid(); 
      var collection = new FormCollection { { "Id", idToUse.ToString() }, { "Amount_" + idToUse, "99" } }; 
      var actual = (RedirectResult)webshopController.PaymentStepOne(collection); 

      Assert.AreEqual("http://www.google.nl", actual.Url); 
     } 

我期待該方法返回一個URL,在這種情況下應該存儲在名爲'actual'的變量中。但是,每當我運行測試,我收到以下錯誤信息;

Test method Plugin.Webshop.Tests.FrontendWebshopControllerTest.PaymentStepOne_Should_Return_RedirectUrl_Because_CustomerId_In_Session_Is_Null threw exception: 
System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. Het systeem kan het opgegeven bestand niet vinden.Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll 
Running under executable C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\QTAgent32.exe 
--- A detailed error log follows. 

=== Pre-bind state information === 
LOG: User = rob 
LOG: DisplayName = System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 
(Fully-specified) 
LOG: Appbase = file:///C:/Projects/Website_v1\SITE/TestResults/WSRob 2010-08-20 14_00_26/Out 
LOG: Initial PrivatePath = NULL 
Calling assembly : Plugin.Webshop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. 
=== 
LOG: This bind starts in default load context. 
LOG: Using application configuration file: C:\Projects\IWES5\IWES\TestResults\WSRob 2010-08-20 14_00_26\Out\Plugin.Webshop.Tests.DLL.config 
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. 
LOG: Post-policy reference: System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 
LOG: Attempting download of new URL file:///C:/Projects/Website_v1\SITE/TestResults/WSRob 2010-08-20 14_00_26/Out/System.Web.Mvc.DLL. 
LOG: Attempting download of new URL file:///C:/Projects/Website_v1\SITE/TestResults/WSRob 2010-08-20 14_00_26/Out/System.Web.Mvc/System.Web.Mvc.DLL. 
LOG: Attempting download of new URL file:///C:/Projects/Website_v1\SITE/TestResults/WSRob 2010-08-20 14_00_26/Out/System.Web.Mvc.EXE. 
LOG: Attempting download of new URL file:///C:/Projects/Website_v1\SITE/TestResults/WSRob 2010-08-20 14_00_26/Out/System.Web.Mvc/System.Web.Mvc.EXE. 

時被調用,我要測試的功能此錯誤occures,所以只要下面的規則正在被測試擊中發生;

var actual = webshopController.PaymentStepOne(collection); 

連同錯誤信息,我發現了以下堆棧跟蹤

Plugin.Webshop.Controllers.FrontendWebshopController.PaymentStepOne(FormCollection collection) 
FrontendWebshopControllerProxy3eebc7d7c86c40848deab621477897c6.InvocationPaymentStepOne_9.InvokeMethodOnTarget() 
Castle.DynamicProxy.AbstractInvocation.Proceed() 
Moq.Interceptor.Intercept(IInvocation invocation) 
Castle.DynamicProxy.AbstractInvocation.Proceed() 
FrontendWebshopControllerProxy3eebc7d7c86c40848deab621477897c6.PaymentStepOne(FormCollection collection) 
Plugin.Webshop.Tests.FrontendWebshopControllerTest.PaymentStepOne_Should_Return_RedirectUrl_Because_CustomerId_In_Session_Is_Null() in C:\Projects\Website_v1\SITE\Plugin.Webshop.Tests\FrontendWebshopControllerTest.cs: line 197 

請通過找到這個錯誤的原因助陣,並解決它。

+0

愚蠢的問題,但你的測試是否包含對System.Web.MVC的引用?或者,您可以嘗試通過右鍵單擊某個方法並選擇「創建單元測試」來創建單元測試項目。這樣你可以確定項目設置正確。 – 2010-08-20 12:50:18

+0

@Igor也回答喬恩。是的,參考在那裏。我得到基本的測試生成,因爲我使用單元測試嚮導添加測試,所以我很確定問題不能在參考 – Rob 2010-08-20 12:55:45

+0

你有混合編譯x86和x64下的程序集? – 2010-08-20 13:19:54

回答

0

我解決了這個問題。我正在測試的函數使用XVal來拋出RulesExceptions。我知道xval並不適用於MVC2,它使用了MVC1中的組件。而且,因爲我正在使用新安裝的機器,所以尚未安裝MVC1。所以我下載了MVC1,安裝完成後我的測試成功了。

有一點還是很奇怪。其他測試中,我的同事和我正在測試RulesExceptions的拋出一直都成功。那麼爲什麼這個測試不是沒有安裝MVC1對我來說仍然是一個大問題。如果有人能告訴我爲什麼我會很高興聽到它。

感謝您的幫助和思考!

1

您是否從測試項目以及您的生產項目中參考了MVC組件?程序集引用不是以傳遞方式處理的。

+0

是所有必需的引用都是 – Rob 2010-08-20 12:49:46

+0

@Rob:因此,如果您在輸出目錄中查看測試程序集,可以看到System.Web.Mvc.dll? (或者它在GAC中?我永遠不會記得......) – 2010-08-20 13:20:10

+0

我在輸出目錄中看不到該DLL。所以我認爲它來自GAC – Rob 2010-08-20 13:35:32