I have 5 assemblies in my solution: A , B , B.Test , C and C.Test . B and C both A links (and do not link to each other). B.Test links A and B , C.Test links A and C
In B.Test I create an EntityFramework6 DbContext object defined in B :
[TestMethod] public void TestB() { MyBContext c = new MyBContext(); }
In C.Test , I have an empty unit test with DeploymentItem :
[TestMethod] [DeploymentItem("data.txt")] public void TestC() { }
When I run two tests separately, they both pass. HOWEVER, When I run all the tests together as part of the same test run, TestB fails with the following exception:
"Entity platform provider type" System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer ', registered in the application configuration file for the ADO.NET provider with the invariant name "System.Data.SqlClient", cannot be loaded. Ensure that the name for the assembly is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for details.
When I modify TestC to comment on the DeploymentItem attribute as follows:
[TestMethod]
Both tests pass now . TestB creates a context without exception. Be that as it may, adding a DeploymentItemAttribute in the C.Test assembly interrupts a test that does not use the DeploymentItem in a separate B.Test assembly (And I have other tests in C.Test that use the DeploymentItem, so deleting this instance does not remove the link from the library). It took me a long time to even narrow down the failure so far, and I am completely confused by what even to do to solve this problem.
EDIT: I found some information about MSDN that seems to solve this problem (although I don't understand why).
- Running unit tests through a Resharper test runner, or another test runner, seems to solve the problem. Only when I run unit tests through VisualStudio (in 2012, if that matters), the tests are not executed
Adding the following code to my assembly that defines DbContext seems to fix the problem:
static MyDbContext () { var _ = typeof(System.Data.Entity.SqlServer.SqlProviderServices); }
(The answer to MSDN suggests adding this to the context class itself, I added it to the Factory type for that context instead of the same useful results).
So it looks like we have the answer to the question "how can I get my unit tests to work?" but not the question "why does this happen in the first place?" I suspect that with this solution in mind, EF6 plays a bit quickly and freely when loading types and assemblies dynamically, and some assemblies do not load when tests are run from certain types of places.