How to get test initialize in memory and use in each test

How to get test initialize in memory and use in each test

I'm trying to create Unit Test. I have class User:

I'm trying to create Unit Test. I have class User:

 public class User
{
    public int UsersCount
    {
        get
        {
            using (MainContext context = new MainContext())
            {
                return context.Users.Count();
            }
        }
    }
    public Guid Id { get; set; } = Guid.NewGuid();
    public string UserName { get; set; }
    public string Password { get; set; }
    public Contact UserContact { get; set; }
}

My first test is UsersCount_Test test which tests UsersCount property:

 [TestMethod]
    public void UsersCount_Test()
    {
        var user = new User();
        var context = new MainContext();
        int usersCount = context.Users.Count();
        context.Users.Add(new User());
        context.SaveChanges();
        Assert.AreEqual(usersCount + 1, user.UsersCount, $"It should be {usersCount + 1} because we're adding one more user");
    }

If I add new test method in my test class (I'm using separate classes for testing each entity), I need to create new instance of User. That's why I did this:

    public class BaseTest<T>
{
    public T TestEntity;

public MainContext TestContext = new MainContext();

}

Now each test classes inherits from this class. And also I created test initializer method. Now my test class looks like this :

 [TestClass]
public class UserTest : BaseTest<User>
{
    [TestMethod]
    public void UsersCount()
    {
        int usersCount = TestContext.Users.Count();
        TestContext.Users.Add(new User());
        TestContext.SaveChanges();
        Assert.AreEqual(usersCount + 1, TestEntity.UsersCount, $"It should be {usersCount + 1} because we're adding one more user");
    }

[TestInitialize]
public void SetTestEntity()
{
    TestEntity = new User();
}

}

Now I'm adding new property to User and writing some logic:

  string phoneNumber;
    public string PhoneNumber { get { return phoneNumber; } set { SetUserContact(phoneNumber, value); phoneNumber = value; } }

void SetUserContact(string oldContact, string newContact)
{
    UserContact.ContactsList.Remove(oldContact);
    UserContact.ContactsList.Add(newContact);
}

After that I'm creating new test :

     [TestMethod]
    public void ContactList_Test()
    {
        var newPhone = "+8888888888888";
        TestEntity.PhoneNumber = newPhone;
        Assert.IsTrue(TestEntity.UserContact.ContactsList.Any(a => a == newPhone), $"It should contains {newPhone}");
    }

Test fails because UserContact of TestEntity is null. I understood that TestEntity should be created by logic. After that I fix test initilizer method:

 [TestInitialize]
    public void SetTestEntity()
    {
        TestEntity = new User() { UserContact = new Contact() };
    }

Here is Contact model

    public class Contact
{
    public Guid Id { get; set; } = Guid.NewGuid();
    public virtual List<string> ContactsList { get; set; } = new List<string>();
}

My question is how to set TestEntity only one time, is it possible (maybe get it in memory and use it when it calls SetTestEntity method)? Because SetTestentity method creates new entity in each test and it takes more development time. (For example If creating instance of UserContact takes 3 seconds all, test runs more than 3 seconds). Another way in this case is to set UserContact in ContactLists test, but I think it's not good idea, in a future when we will add new logics I need to fix each test. Please give me any suggestion, ideas.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

C/C++ vs. Rust: A developer’s perspective

In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective

Variable Introduction in C#[Bangla]

LIKE | COMMENT | SHARE | SUBSCRIBE A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a speci...