Testing a device using Microsoft EntityFramework

Currently, I have a small individual project that I want to start with the right foot, which means that my functions are checked per unit. My application usually uses a data context S_ERP_DBEntities, but when testing, I want to change the data context to Test_S_ERP_DBEntities. This is what I have, but I'm really not sure. The default view calls the default constructor, which means that I would have the correct data context if it had not been tested, since the default constructor does not change it.

LoginViewModel.cs

private object dbContext = new S_ERP_DBEntities();

    public LoginViewModel(object dbEntities)
    {
        dbContext = dbEntities;
    }

Unittest1.cs

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var dbContext = new Test_S_ERP_DBEntities();
        var login = new LoginViewModel(dbContext);
    }
}

This will work, but I don't know if there is a more elegant way to do this. Any feedback would be appreciated.

thanks

+4
1

, , , Dependency Injection. , , dbContext , :

private object dbContext;

public LoginViewModel()
{
    dbContext = new S_ERP_DBEntities();
}

public LoginViewModel(object dbEntities)
{
    dbContext = dbEntities;
}

, "" , , dbContext , - , " ",.

, dbContext object. , object, : S_ERP_DBEntities Test_S_ERP_DBEntities. , , , . , , , (: ) S_ERP_DBEntities . : , Microsoft Entity Framework, , . . Mock with Entity Framework.

+3

Source: https://habr.com/ru/post/1614086/


All Articles