tags:

views:

1489

answers:

7

Using ASP.NET MVC 1.0 (current) I create a new default ASP.NET MVC project using Visual Studio 2008 on an x64 machine (Server 2008) and accept all the defaults and build and run it. Apart from having to set the System.Web.* assemblies as "Copy Local" it runs and brings up the default web app. When I try and run the unit tests on this project I get:

Unit Test Adapter threw exception: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information..

Now if I repeat the same exercise using VS2008 on an x86 machine (Server 2003) then all 27 default unit tests run fine. (Also I don't need to mark the System.Web.* assemblies as "Copy Local")

Ideas for resolving the exception?

More Info: After trying some of the solutions suggested I started commenting out parts of the boilerplate test code that is generated with a new project. As such, I believe that the error is being generated by the inclusion of one of the following classes:

    public class MockIdentity : IIdentity
    {
        public string AuthenticationType
        {
            get
            {
                return "MockAuthentication";
            }
        }

        public bool IsAuthenticated
        {
            get
            {
                return true;
            }
        }

        public string Name
        {
            get
            {
                return "someUser";
            }
        }
    }

    public class MockPrincipal : IPrincipal
    {
        IIdentity _identity;

        public IIdentity Identity
        {
            get
            {
                if (_identity == null)
                {
                    _identity = new MockIdentity();
                }
                return _identity;
            }
        }

        public bool IsInRole(string role)
        {
            return false;
        }
    }

    public class MockMembershipUser : MembershipUser
    {
        public override bool ChangePassword(string oldPassword, string newPassword)
        {
            return newPassword.Equals("newPass");
        }
    }

    public class MockHttpContext : HttpContextBase
    {
        private IPrincipal _user;

        public override IPrincipal User
        {
            get
            {
                if (_user == null)
                {
                    _user = new MockPrincipal();
                }
                return _user;
            }
            set
            {
                _user = value;
            }
        }
    }
+1  A: 

Do you have VS 2008 SP1 and .NET 3.5 SP1 installed in your Win2k8 box? By default VS 2008 installed 3.5 framework, but NOT SP1. Make sure you have both the framework SP1 and VS 2008 SP1 installed.

Johannes Setiabudi
Yes - got both installed - thanks for the idea though.
Guy
+1  A: 

First, have you tried it in Release configuration? Have you done a clean on your solution?

Have you tried constructing your test project? Take out the source files for your tests, delete the project from your solution and add a new test project that references the MVC app. Then re-add the test source files.

Edit

Are the classes and interfaces that you are using and implementing in scope from your tests?

Edit Is it referencing the x64 not the x86 dlls?

Daniel A. White
Thanks for the ideas Daniel but still no luck.
Guy
Yes - remember that this all works when the project is checked out on an x86 machine.
Guy
I don't know - how do I tell?
Guy
Doesn't the debugger show what dll's are loaded?
Daniel A. White
During the running of the unit tests? If so, where would I see them?
Guy
+1  A: 

Did you try building your project as x86 project (Project properties ->Build ->Platform target) on x64 machine?

Also, not sure about your unit testing toolkit, but NUnit, for example, can run as tests as either x86 or x64 (on x64 machine). If one of your assemblies accesses some 32-bit code (e.g. COM object), trying to run them under x64 will result in "Unable to load one or more of the requested types" error.

bh213
I'm running MSTest - the default unit tester that comes with VS2008. I've tried building the projects as both x86 and x64. Thanks for the help.
Guy
Well, if x86/x64 is not an issue, then should only leave actual assembly files.I'd copy all files needed (Dlls) to a new directory and try running using MSBuild command line. When it works on x86 machine, just copy over to x64 and see if it works there.
bh213
Can you explain that in more detail? I don't exactly follow what you'd do here...
Guy
+1  A: 

I'm not sure about the subtleties of what's going on with the x64 verus x86, but using custom identity/principals can cause some interesting little glitches to happen, especially if using cassini (the built-in vshost webserver - which I think is what you end up using by running local unit tests within VS). I ran into this issue before, and rather than detailing it here, I'll post a link to some good info. Again, I'm not sure if this is related to your problem (my not being an MVC guru), but take a read through this. Food for thought:

http://www.lhotka.net/weblog/UpdateOnMyStrugglesWithTheASPNETDevelopmentServer.aspx

EDIT: so ultimately, this may be an issue of serialization failure, even if this particular edge case is not relevant. Have you tried marking your mocked iidentity/iprincpal objects as [serializable]? Remember that visual studio is a 32bit application; perhaps testing it on IIS (if not cassini) with a 64bit application pool is causing a context switch somewhere which causes the mock identities (if they get assigned as a thread's identity) to get marshalled across a boundary like that - the lack of a [serializable] attribute will probably cause a TypeLoadException.

Does it still throw if you set IIS to use a 32bit application pool (on your 64bit server)?

-Oisin

x0n
Is IIS involved when you're running unit tests from VS2008? I didn't think that it was...? From my understanding unit tests don't spin up Cassini or use IIS...
Guy
sorry, yeah, you're surely right. I wasn't really thinking about the unit test stuff - that runs as a standard library project, right? I was thinking for a moment that those tests ran inproc with the webserver... i'm not really a unit test guy (yet)
x0n
A: 

I'm wondering if the more significant difference is 2k8 vs 2k3 than 64 vs 32bit. Did you spawn Visual Studio as Administrator? An assembly might be missing due to the Virtual Store of windows 2k8. If this is a development desktop running 2k8 you might want to consider disabling the Virtual Store, it's in you policy labeled as "User Account Control: Virtualize file and registry write failures to per-user locations"

Jeff Mc
Yes I am running VS2008 as Admin. Yes it's a 2k8 server being run as a desktop development environment. UAC is disabled on this machine.
Guy
A: 

I notice some of the types (specifically IIdentity and IPrincipal) you are implementing are not located within a System.Web.* assembly.

Have you tried marking the System.Security.Principal assembly as "Copy Local"?

John Rasch
That assembly is not being referenced in the project so not possible to mark as Copy Local.
Guy
A: 

This reminded me of an old blog post I once read. Perhaps you can use the same technique to debug the cause of the error:

http://www.agileprogrammer.com/oneagilecoder/archive/2007/11/17.aspx

VVS