lundi 14 décembre 2015

Is it bad idea to use Dependency Injection objects in unit tests?

I am not sure if what i am doing is actually the "correct" way of doing unit tests with DI. Right now i ask my ViewModelLocator to actually create all the instances i need, and just get the instance i need to test, which makes it very simple to test a single instance because lets asume that Receipt needs a Reseller object to be created, reseller needs a User object to be created, user need some other object to be created, which creates a chain of objects to create just to test one single instance.

With di usally interfaces will get mocked and parsed to the object which you would like to create, but how about simple Entities/ViewModels?

Whats the best practice to do unit testing with DI involved?

 public class JournalTest
{
    private ReceiptViewModel receipt;
    private ViewModelLocator locator;
    [SetUp]
    public void SetUp()
    {
        locator = new ViewModelLocator();

        receipt = SimpleIoc.Default.GetInstance<ReceiptViewModel>();
    }
    [TearDown]



    [Test]
    public void CheckAndCreateNewJournal_Should_Always_Create_New_Journal()
    {
        receipt.Sale.Journal = null;
        receipt.Sale.CheckAndCreateNewJournal();
        Assert.NotNull(receipt.Sale.Journal);
    }

}

Aucun commentaire:

Enregistrer un commentaire