开发者

C# Unit Test error, could not load file or assembly when running test

开发者 https://www.devze.com 2023-01-12 15:51 出处:网络
I\'m working on a unit test in a project using the MOQ-framework, C# 4.0, MVC2.0 The test is similar to the one below. But as soon as i run this test i\'m getting a strange error. I\'ve checked all t

I'm working on a unit test in a project using the MOQ-framework, C# 4.0, MVC2.0

The test is similar to the one below. But as soon as i run this test i'm getting a strange error. I've checked all the referenced System.Web.mvc assemblies and there all at version 2.0.0, so it seems to me that those can't cause the issue.

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

I'm expecting the method to return an URL in this scenario which is supposed to be stored in the variable called 'actual'. But whenever i run the test i'm getting the following error message;

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 : Pl开发者_如何学Gougin.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.

This error occures when the function which i want to test is being called, so it happens as soon as the following rule is being hit by the test;

var actual = webshopController.PaymentStepOne(collection);

Together with the error message i'm getting the following 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

Please help out by finding the cause of this error, and resolving it.


Do you have a reference to the MVC assemblies from your test project as well as from your production project? Assembly references aren't handled transitively.


I solved the problem. The function I'm testing was using XVal to throw RulesExceptions. I know that xval doesn't work with MVC2 yet and it uses components from MVC1. And since I was working on a freshly installed machine MVC1 hasn't been installed yet. So I downloaded MVC1 and after installation my test was succeeding.

One thing is still strange. Other tests where my colleagues and I were testing the throwing of RulesExceptions did succeed all the time. So why this test didn't without the installation of MVC1 is still a big question for me. If anyone can tell my why I'd be happy to hear it.

0

精彩评论

暂无评论...
验证码 换一张
取 消