none
Unit Test and Entity Framework RRS feed

  • Question

  • Hi All

     

    Is it possible to build a unit test with Entity Framework? 

     

    I have tried to copy the csdl, msdl, ssl to .\bin folder

     

    and update the web.config under the test project.

     

    but following error still occurs :-

     

    Test method HOME.DAL.Test.UserServiceTest.registerUserTest threw exception:  System.ArgumentException: The given connection string is not valid because it contains insufficient mapping or metadata information.
    Parameter name: connectionString --->  System.InvalidOperationException: '~' is only valid in metadata file paths when running inside ASP.NET.. 

     

    regards

    Alex

     

     

     

     

    Monday, April 28, 2008 3:13 AM

Answers

  • You will probably need to rewrite the part of you connection string that points to metadata in a way that works when you are outside the ASP.NET environment.

     

    "Regular" paths relative to your execution path should work. Something like "metadata=.\metadatadir\blah.csdl|.\metadatadir\blah.msl|.\metadatadir\blah.ssdl..." or even "metadata=.\metadatadir\*..." should work.

     

    How does your current connection string looks like?

     

    There are several other considerations with EF in unit tests. It would be interesting to hear more from your experience.

     

    Hope this helps,

    -Diego 

    Monday, April 28, 2008 3:46 AM
    Moderator

All replies

  • You will probably need to rewrite the part of you connection string that points to metadata in a way that works when you are outside the ASP.NET environment.

     

    "Regular" paths relative to your execution path should work. Something like "metadata=.\metadatadir\blah.csdl|.\metadatadir\blah.msl|.\metadatadir\blah.ssdl..." or even "metadata=.\metadatadir\*..." should work.

     

    How does your current connection string looks like?

     

    There are several other considerations with EF in unit tests. It would be interesting to hear more from your experience.

     

    Hope this helps,

    -Diego 

    Monday, April 28, 2008 3:46 AM
    Moderator
  •  

    Maybe I can share a little bit about what I did. It's a normal unittest project though, not asp.net specific.

     

     

    I have an app.config in the unittest project rewritten like this:

     

    Code Snippet

    connectionString="metadata=InheritanceType.csdl|InheritanceType.ssdl|InheritanceType.msl;provider=Syst......

     

     

    As you can see, no path set, just straight pointing to the files.

    Then, in the testrunconfig, I set them as deployment items:

     

    Code Snippet

    <Deployment>

    <DeploymentItem filename="Domain\bin\ServerSide\InheritanceType.msl" />

    <DeploymentItem filename="Domain\bin\ServerSide\InheritanceType.ssdl" />

    <DeploymentItem filename="Domain\bin\ServerSide\InheritanceType.csdl" />

    </Deployment>

     

     

    This will copy the files to the unittest dir, so you can use them.

    I've also experimented with specific output directories. For instance, say you want to use the exact same appconfig call and in the regular appconfig, you point to a folder: connectionString="metadata=EFTest/xxx.csdl".

    It is possible to create a EFTest folder automatically while deploying, by looking at your testrunconfig with the xmleditor and setting the 'outputDirectory' property. This is not possible through the GUI, so you need to go through xml editor here.

     

     

    I hope that helps

    Monday, April 28, 2008 8:24 AM