mardi 28 juin 2016

How to mock the functionality of the test class itself using Moq in ASP.NET MVC unit test

I am developing an ASP.NET MVC project. In my project, I am doing unit testing. I use Moq for unit test. But I am having a problem with testing a method of a model class that has dependency on another function of its. Please see my scenario below.

This is the sample of model class that I want to test

public class ItemRepo:IItemRepo
{
    public IEnumerable<Item> GetItems()
    {
        //do something
        return items;
    }

    public string GenerateItemCode()
    {
       Item item = this.GetItems.OrderByDescending(x=>x.Id).FirstOrDefault();
       //do something
       return itemCode;
    }
}

I want to test GenerateItemCode method

    [TestClass]
    public class ItemRepoTests
    {
        [TestMethod]
        public void GenerateItemCode_IncreaseDigit()
        {
            Item[] items = new Item[]{
                new Item{
                    ItemCode = "DN999934"
                }
            };
            ItemRepo itemRepo = new ItemRepo();
            //I want to mock GetItems method here
        }
    }

I commented what I want to mock in test code. So please how can I mock that method? Please help me. How can I unit test that method mocking dependency function?

Aucun commentaire:

Enregistrer un commentaire