0

I am writing some tests. I am mocking entity framework. Yesterday worked everything correctly and I am almost sure I have not modified anything from yesterday. In the app.config of my test project I have this connection string

<connectionStrings>
  <add name="PublicAreaContext"
      providerName="System.Data.SqlClient" connectionString="Server=.\SQLSERVER; ..." />
</connectionStrings>

Now I mock my entity framework context:

var mockContext = new Mock<PublicAreaContext>() { CallBase = true };
... // all my entities mocked
mockContext.Setup(x => x.SaveChanges()).Returns(1);

Now when I start my test something goes wrong, and the context seems to be created using the wrong connection string

enter image description here

I do not understand where it takes that connection string... I looked for it in the solution SQLEXPRESS and I do not find anything. In my server (SQLSERVER) I can find a database called Castle.Proxy.PublicAreaContext. I think is the database created yesterday when everything worked.

Can you tell me what can be the problem or how can I set correctly the connection string?

Thank you

Simone
  • 2,304
  • 6
  • 30
  • 79
  • 2
    Can you show to code that is actually using the connection string and the code passing it in? – Matt Rowland Jul 10 '16 at 14:49
  • It's probably because of `CallBase` that is invoking the default `DbContext` constructor that unless passed a database or connection-string name, will try to create one with the name of the context type. – haim770 Jul 10 '16 at 14:56
  • @haim770: if I set `CallBase = false` I obtain this error: `The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.` – Simone Jul 10 '16 at 15:04
  • 1
    Related - http://stackoverflow.com/questions/6766478/unit-testing-dbcontext, http://stackoverflow.com/questions/5625746/generic-repository-with-ef-4-1-what-is-the-point – Eugene Podskal Jul 10 '16 at 15:07
  • I have not understood which code @MattRowland asked me.. I think I do not have that code because the context has the same name of the connection string... – Simone Jul 10 '16 at 15:09
  • It is confusing for the reader. You trying to talk to a database and messing up with the configuration. Therefore this is NOT a Unit Test. It is an INTEGRATION TEST. – Spock Jul 13 '16 at 00:06
  • 1
    @Spock I have changed... I eliminated the word "unit" from my post. now it's clearer – Simone Jul 13 '16 at 10:01

1 Answers1

0

I've solved...

I think that when I mock the context, the context change its name... so no connectionString is found... I have changed my context adding the following constructor:

public partial class PublicAreaContext : DbContext, IDataContext
{
    public PublicAreaContext() 
        : base("PublicAreaContext") //Here go the name of connection string
    {

    }

    ...
}
Simone
  • 2,304
  • 6
  • 30
  • 79