Click here to Skip to main content
15,443,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some tests on xUnit where during the test a DBInstance is created based on a randomly generated name

private static readonly string _databaseInstanceName = Guid.NewGuid().ToString("N")

private static readonly string _databaseConnection = $@"Server=(localdb)\{_databaseInstanceName};Integrated Security=true";


Then an instance is created as follows
public LocalDbManager LocalDbManager = new LocalDbManager(_databaseInstanceName);


At the time being as the code is a random GUID is generated only once, but now I need that this random GUID is generated for every DB instance I create.

What I have tried:

But my problem is that some tests are failing with this approach

private static string _databaseInstanceName;

private static string GetDbInstanceName()
{
	_databaseInstanceName = Guid.NewGuid().ToString("N");
	return _databaseInstanceName;
}

public LocalDbManager LocalDbManager = new LocalDbManager(GetDbInstanceName());

private string DatabaseConnection => $@"Server=(localdb)\{_databaseInstanceName};Integrated Security=true";
Posted
Updated 2-Mar-21 1:46am
v2
Comments
CHill60 2-Mar-21 7:44am    
Failing how?

1 solution

Make it a property instead of a field.

C#
private static string _databaseInstanceName 
{ 
    get { return Guid.NewGuid().ToString(); } 
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900