dimanche 1 novembre 2015

Unit Testing with Microsoft EntityFramework

I have a currently small personnal project that I want to start off on the right foot which means making sure that my fonctions are unit tested. My application normally use the data context S_ERP_DBEntities but when testing I'd want to change the data context to Test_S_ERP_DBEntities. This is what I have so far but i am really not sure. The view by default calls the default constructor which means that I'd have the right data context when not testing since the default constructor doesn't 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);
    }
}

That would work but I don't know if there is a more elegant way of doing it. Any feedback will be appreciated.

Thanks

Aucun commentaire:

Enregistrer un commentaire