【问题标题】:C# Unit Test error, could not load file or assembly when running testC#单元测试错误,运行测试时无法加载文件或程序集
【发布时间】:2010-08-20 12:43:16
【问题描述】:

我正在使用 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 应该存储在名为“实际”的变量中。但是每当我运行测试时,我都会收到以下错误消息;

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);

连同错误消息,我得到以下堆栈跟踪

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

请帮忙找出这个错误的原因并解决它。

【问题讨论】:

  • 愚蠢的问题,但是您的测试是否包含对 System.Web.MVC 的引用?或者,您可以尝试通过右键单击方法并选择“创建单元测试”来从控制器中创建单元测试项目。这样您就可以确保项目设置正确。
  • @Igor 也回答了乔恩。是的,参考资料在那里。我生成了基本测试,因为我使用单元测试向导添加了测试,所以我很确定问题不在参考文献中
  • 你有混合在 x86 和 x64 下编译的程序集吗?
  • @Jerod。不,我没有。所有程序集都在 x64 机器上编译

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


【解决方案1】:

您的 test 项目以及生产项目中是否引用了 MVC 程序集?程序集引用不被传递处理。

【讨论】:

  • 是的,所有必需的参考资料都已完成
  • @Rob:因此,如果您查看测试程序集的输出目录,您可以看到 System.Web.Mvc.dll? (或者它在 GAC 中?我不记得了......)
  • 我在输出目录中看不到 DLL。所以我认为它来自 GAC
  • @Rob:嗯。它在引用属性窗口中作为路径显示什么?
  • 它引导我到程序文件目录,其中 MVC2 目录与程序集一起位于该目录。通过在我的机器上安装 MVC1,我能够解决这个问题。有关更多详细信息,请参阅我的答案。感谢您与我一起思考!
【解决方案2】:

我解决了这个问题。我正在测试的函数是使用 XVal 来抛出 RulesExceptions。我知道 xval 还不能与 MVC2 一起使用,它使用 MVC1 中的组件。而且由于我正在使用新安装的机器 MVC1 尚未安装。所以我下载了MVC1,安装后我的测试成功了。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 2013-03-22
    • 2023-03-29
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    相关资源
    最近更新 更多