Asp.net mvc - How to quickly and efficiently create fake test objects

I am currently testing a controller in my mvc application and I am creating a fake repository for testing. However, I seem to be writing more code and spending more time on fakes than on actual repositories. Is it correct?

The code I have is as follows:

controller

public partial class SomeController : Controller
{
    IRepository repository;

    public SomeController(IRepository rep)
    {
        repository = rep;
    }

    public virtaul ActionResult Index()
    {
        // Some logic
        var model = repository.GetSomething();

        return View(model);
    }
}

IRepository

public interface IRepository
{
    Something GetSomething();
}

Fake repository

public class FakeRepository : IRepository
{
    private List<Something> somethingList;

    public FakeRepository(List<Something> somethings)
    {
        somthingList = somthings;
    }

    public Something GetSomething()
    {
        return somethingList;
    }
}

Fake Data

class FakeSomethingData
{
    public static List<Something> CreateSomethingData()
    {
        var somethings = new List<Something>();

        for (int i = 0; i < 100; i++)
        {
            somethings.Add(new Something
            {
                value1 = String.Format("value{0}", i),
                value2 = String.Format("value{0}", i),
                value3 = String.Format("value{0}", i)
            });
        }

        return somethings;
    }
}

Actual test

[TestClass]
public class SomethingControllerTest
{
    SomethingController CreateSomethingController()
    {
        var testData = FakeSomethingData.CreateSomethingData();
        var repository = new FakeSomethingRepository(testData);

        SomethingController controller = new SomethingController(repository);

        return controller;
    }

    [TestMethod]
    public void SomeTest()
    {
        // Arrange
        var controller = CreateSomethingController();

        // Act
        // Some test here

        // Arrange
    }
}

All this seems to be a lot of additional code, especially since I have several repositories. Is there a more efficient way to do this? Maybe using mocks?

thank

+3
source share
3 answers

CD, . Moq, Moq - :

// Arrange
var repoMock = new Mock<IRepository>();
repoMock.Setup(r => r.GetSomething()).Returns(TestData.SomeThings);
var controller = new SomethingController(repoMock.Object);

// Act
controller.DoStuff();

// Assert
...

TestData - , . , TestData :

public static List<Something> SomeThings
{ 
    get
    {     
        var somethings = new List<Something>();

        for (int i = 0; i < 100; i++)
        {
            somethings.Add(new Something
            {
                value1 = String.Format("value{0}", i),
                value2 = String.Format("value{0}", i),
                value3 = String.Format("value{0}", i)
            });
        }

        return somethings;
    }
}
+1

.

( Moq, Moq)

+2

Dev Magic Fake, Mock , Mock UI

DevMagicFake.dll

:

 [HttpPost]
        public ActionResult Create(VendorForm vendorForm)
        {
            var repoistory = new FakeRepository<VendorForm>();
            repoistory.Save(vendorForm);
            return View("Page", repoistory.GetAll());
        }

This will save VendorForm persistent memory in memory, and you can get it anytime. You can also generate data for this object or any other object in your model; for more information about Dev Magic Fake, see the following CodePlex link:

http://devmagicfake.codeplex.com

thank

M.Radwan

0
source

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


All Articles