Question

I have a Test that i am running from NUnit but it keeps failing

    [Test]
    public void CheckTeamsExist()
    {
        UsersContext context = new UsersContext();

        var count = context.Events.Count();

        Assert.AreSame(true, count > 10);

    }

The exact error message i am getting is

QUBBasketballMVC.Tests.Controllers.TeamContextTest.CheckTeamsExist: System.Data.EntityException : The underlying provider failed on Open. ----> System.Data.SqlClient.SqlException : An attempt to attach an auto-named database for file C:\Users\connor\Desktop\dissertation_project (ASSEMBLA)\branches\interim_demo\Qub_Website\QUBBasketballMVC.Tests\bin\Debug\qub_db.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

    System.Data.EntityException was unhandled by user code
      HResult=-2146233087
      Message=The underlying provider failed on Open.
      Source=System.Data.Entity
      StackTrace:
           at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
           at System.Data.EntityClient.EntityConnection.Open()
           at System.Data.Objects.ObjectContext.EnsureConnection()
           at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
           at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
           at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
           at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__3[TResult](IEnumerable`1 sequence)
           at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
           at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
           at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
           at QUBBasketballMVC.Tests.Controllers.AccountControllerTest.ExampleForMockingAccountMembershipServiceNEW() in c:\Users\connor\Desktop\dissertation_project (ASSEMBLA)\branches\interim_demo\Qub_Website\QUBBasketballMVC.Tests\Controllers\AccountControllerTest.cs:line 109
      InnerException: System.Data.SqlClient.SqlException
           HResult=-2146232060
           Message=An attempt to attach an auto-named database for file C:\Users\connor\Desktop\dissertation_project (ASSEMBLA)\branches\interim_demo\Qub_Website\QUBBasketballMVC.Tests\bin\Debug\qub_db.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
           Source=.Net SqlClient Data Provider
           ErrorCode=-2146232060
           Class=14
           LineNumber=65536
           Number=15350
           Procedure=""
           Server=(LocalDB)\v11.0
           State=1
           StackTrace:
                at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
                at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
                at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
                at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
                at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
                at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
                at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
                at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
                at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions)
                at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
                at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
                at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions)
                at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions)
                at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
                at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
                at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
                at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
                at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
                at System.Data.SqlClient.SqlConnection.Open()
                at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
           InnerException: 

Can't see where i'm going wrong! This code works fine from my main project

<connectionStrings>
<add name="qub_dbEntities1" connectionString="metadata=res://*/Models.entityDataModel.csdl|res://*/Models.entityDataModel.ssdl|res://*/Models.entityDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\qub_db.mdf;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

</connectionStrings>

I have this connection string in both config files.

Is the problem that DataDirectory/location of entity framework files is not the same in my unit test project?

Était-ce utile?

La solution

To add to Lilshieste's comment, Replace the DataDirectory place holder in the app.config of the test project with a hard coded path to the App Data folder.

It also works if you copy the database files (mdf & ldf) to the bin/debug folder of the test project.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top