C#单元测试错误,无法在运行测试时加载文件或程序集

时间:2010-08-20 12:43:17

标签: c# .net asp.net unit-testing moq

我正在使用MOQ框架,C#4.0,MVC2.0进行项目中的单元测试

测试类似于下面的测试。但是一旦我运行这个测试,我就会收到一个奇怪的错误。我已经检查了所有引用的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,该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.

当我要调用我要测试的函数时会发生此错误,因此只要测试命中以下规则就会发生错误;

var actual = webshopController.PaymentStepOne(collection);

与错误消息一起,我得到以下Stack Trace

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

请通过查找此错误的原因并解决它来帮助解决。

2 个答案:

答案 0 :(得分:1)

您是否从 test 项目以及生产项目中引用了MVC程序集?装配参考不是可传递的。

答案 1 :(得分:0)

我解决了这个问题。我正在测试的函数是使用XVal抛出RulesExceptions。我知道xval还不能用于MVC2,它使用MVC1中的组件。由于我正在安装新安装的机器,因此还没有安装MVC1。所以我下载了MVC1,安装完成后我的测试成功了。

有一件事仍然很奇怪。我和我的同事测试抛出RulesExceptions的其他测试确实一直成功。那么为什么这个测试没有安装MVC1对我来说仍然是个大问题。如果有人能告诉我为什么我会乐意听到它。

感谢您的帮助和思考!