代码之家  ›  专栏  ›  技术社区  ›  Rob

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

  •  0
  • Rob  · 技术社区  · 14 年前

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

    测试与下面的测试类似。但我一做这个测试就发现一个奇怪的错误。我查了所有的参考资料系统.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);
    

    与错误消息一起,我得到以下堆栈跟踪

    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 回复  |  直到 14 年前
        1
  •  1
  •   Jon Skeet    14 年前

    你是否有一个MVC程序集的引用 项目以及从您的生产项目?程序集引用不能传递处理。

        2
  •  0
  •   Ben Voigt Caesar    13 年前

    我解决了这个问题。我正在测试的函数是使用XVal抛出rulesceptions。我知道xval还不能与MVC2一起工作,它使用MVC1中的组件。由于我在一台新安装的机器上工作,MVC1还没有安装。所以我下载了MVC1,安装之后我的测试成功了。

    有一件事仍然很奇怪。我和我的同事在测试规则概念的抛出的其他测试一直都是成功的。所以为什么这个测试没有安装MVC1对我来说仍然是个大问题。如果有人能告诉我为什么我会很高兴听到。

    谢谢你的帮助和思考!