0

I wanted to test a method that creates an account.

public users CreateAccount(string _username, double _weight, string _password)
{
    using (var db = new db_modelContainer())
    {
        Daily_summary summary = new Daily_summary { weight = _weight};
        Users_dishes_gallery user_dishes = new Users_dishes_gallery { };

        var x = db.usersSet;
        foreach (var i in x)
        {
            if (_username == i.name)
            {

                throw new CreateAccountFailException("Username is already occupied!");
            }
        }

        users newuser = new users { name = _username, weight = _weight, password = _password};

        
        db.usersSet.Add(newuser);
        db.SaveChanges();
        return newuser;
    }
}

I have another xUnit project where I wanted to write those tests. I prepared my first file:

[Fact]
//[AutoRollback]
public void CreateAccount_GivenNotOccupiedUsername_CreateSucceed()
{

    string expectedLogin = "test";
    string expectedPassword = "test";
    double expectedWeight = 30;

    Users user = new Users();
    users createduser = user.CreateAccount(expectedLogin, expectedWeight,expectedPassword);
    
    // Assert
    Assert.Equal(expectedLogin, createduser.name);
    Assert.Equal(expectedPassword, createduser.password);
    Assert.Equal(expectedWeight, createduser.weight);

}

But I'm still getting that error.

error from vs

System.InvalidOperationException : No connection string named 'db_modelContainer' could be found in the application config file.

I tried many ways. Main project with edmx inside is my start project. I also added a link to App. config from my main project so my test project has access to the connection string but it didn't help me.

link to app.config

Below is my App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>
  <connectionStrings>
    <add name="db_modelContainer" connectionString="metadata=res://*/Database.db_model.csdl|res://*/Database.db_model.ssdl|res://*/Database.db_model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\SQLEXPRESS;initial catalog=Dietaverse_database;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>
3
  • There is something wrong with your connection string. A connection string is need to interface to the database. There is also a mapping that is used to map the c# classes to the database tables/fields. The mapping requires a connection string named 'db_modelContainer' Commented Oct 15, 2022 at 14:50
  • But my main project normally communicates with db. I also have connection string with a right name. I added my App.config to the question. Commented Oct 15, 2022 at 15:16
  • See following : stackoverflow.com/questions/43780571/… Commented Oct 15, 2022 at 19:36

1 Answer 1

0

Where is your App.Config file? If it's not hosted in the same project as your Unit Test then it probably isn't being loaded into the program. Try making a copy of your App.Config file and adding it to your Unit Test project and setting the property to "Copy Always"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.