views:

183

answers:

2

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 : 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.

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.

A: 

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.

Jon Skeet
Yes all the required references are made
Rob
@Rob: So if you look in the output directory for your test assembly, you can see System.Web.Mvc.dll? (Or is it in the GAC? I can never remember...)
Jon Skeet
@Jon: I don't see the DLL in the output directory. So i think it comes from the GAC
Rob
@Rob: Hmm. What does it show in the references property window as the path?
Jon Skeet
@Jon: It was directing me to the program files dir where an MVC2 directory was located with the assembly. Never the less i was able to solve this issue by installing MVC1 on my machine. See my answer for more details. Thanks for thinking with me!
Rob
A: 

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 me and my colleques where 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'm happy to hear it.

Thanks for helping and thinking with me!

Rob