Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Unit Test against real DB? #2946


User avatar
0
caleb created

Hi

I have a need to to run unit tests against my actual DB and am wondering how to do that. I'd imagine I just need to comment out a few lines in the Tests project, but as I am very new to ABP and Effort, I am not sure what to do.

Any help would be appreciated.

Thanks


2 Answer(s)
  • User Avatar
    0
    vnpedrofilipe created

    create one class like this.... include all table from your database

    public class PRWEBSistemaBaseTesteDB
        {
            private readonly PRWEBDbContext _context;
            private readonly string _connectionString;
    
            public PRWEBSistemaBaseTesteDB(PRWEBDbContext context)
            {
                _connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=PRWEB2;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;";
                //Resolve<IAbpStartupConfiguration>().DefaultNameOrConnectionString = _connectionString;
               
    
                _context = context;
            }
    
            public void Build()
            {
                using (SqlConnection con = new SqlConnection(_connectionString))
                {
                    //
                    // Open the SqlConnection.
                    //
                    con.Open();
                    //
                    // The following code uses an SqlCommand based on the SqlConnection.
                    //
                    using (SqlCommand command = new SqlCommand("SELECT Id, Nome, TenantId, Data FROM Feriados", con))
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            _context.Feriado.Add(
                                new Feriado
                                {
                                    Id= reader.GetInt64(0),
                                    Nome = reader.GetString(1),
                                    TenantId = reader.GetInt32(2),
                                    Data = reader.GetDateTime(3)
                                });
                        }
                    }
                }
            }
        }
    

    and in testBase, change

    //Seed initial data for host
                AbpSession.TenantId = null;
                UsingDbContext(context =>
                {
                    // database fake
                    //new PRWEBSistemaBaseTeste(context).Build();
                    
                    // database 
                    new PRWEBSistemaBaseTesteDB(context).Build();
    
                    new InitialHostDbBuilder(context).Create();
                    new DefaultTenantCreator(context).Create();
                    
                });
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks @vnpedrofilipe :)