单元测试MVC 5控制器使用工作单元和存储库创建操作

时间:2016-11-20 09:38:50

标签: c# asp.net-mvc unit-testing moq

我正在尝试测试接受视图模型并创建新条目的控制器操作。这是控制器动作:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ContactViewModel viewModel)
{
    if (!ModelState.IsValid)
        return View("Create", viewModel);

    _unitOfWork.Contacts.Add(Mapper.Map(viewModel, new Contact()));  

    _unitOfWork.Complete();

    return RedirectToAction("Index");
}

单元测试:

[TestClass]
public class ContactControllerTests
{
    private ContactsController _controller;
    private Mock<IContactRepository> _mockRepository;
    private string _userId;
    private Mock<IUnitOfWork> _mockUoW;

    [ClassInitialize]
    public static void Init(TestContext context)
    {
        Mapper.Initialize(c => c.AddProfile<MappingProfile>());
    }

    [TestInitialize]
    public void TestInitialize()
    {
        _userId = "1";
        _mockRepository = new Mock<IContactRepository>();
        _mockUoW = new Mock<IUnitOfWork>();

        _mockUoW.SetupGet(u => u.Contacts).Returns(_mockRepository.Object);

        _controller = new ContactsController(_mockUoW.Object);

        _controller.MockCurrentUser(_userId, "user@domain.com");
    }

    [TestMethod]
    public void CreatePost_ValidValuesSubmitted_ShouldCallComplete()
    {
        var viewModel = new ContactViewModel()
        {
            FirstName = "a",
            LastName = "b"
        };

        _controller.Create(viewModel);

        _mockRepository.Object.GetContacts(_userId).Should().HaveCount(1);
    }
}

单元测试总是返回计数0,而我期望它是1.我对TDD很新,我实现了工作单元和存储库模式,如Mosh Hamedani的课程所示: https://app.pluralsight.com/library/courses/full-stack-dot-net-developer-architecture-testing/table-of-contents

1 个答案:

答案 0 :(得分:1)

您没有在上面的示例中模拟存储库的任何行为。

根据提供的示例,我们假设一个简单的界面。

public interface IContactRepository {
    void Add(Contact contact);
    IEnumerable<Contact> GetContacts(string _userId);
}

您需要为数据提供某种形式的存储空间。

[TestInitialize]
public void TestInitialize() {
    _userId = "1";
    var data = new List<Contact>();//To store test data.
    //Configure repository
    _mockRepository = new Mock<IContactRepository>();
    _mockRepository.Setup(m => m.Add(It.IsAny<Contact>())).Callback<Contact>(data.Add);
    _mockRepository.Setup(m => m.GetContacts(_userId)).Returns(data);
    //Configure UoW
    _mockUoW = new Mock<IUnitOfWork>();
    _mockUoW.SetupGet(u => u.Contacts).Returns(_mockRepository.Object);

    _controller = new ContactsController(_mockUoW.Object);

    _controller.MockCurrentUser(_userId, "user@domain.com");
}

或放弃模拟并制造假货。

public class FakeContactRepository : IContactRepository {
    private ICollection<Contact> data;

    public FakeContactRepository(ICollection<Contact> data) {                
        this.data = data;
    }

    public void Add(Contact contact) {
        data.Add(contact);
    }

    public IEnumerable<Contact> GetContacts(string _userId) {
        return data;
    }
}

并将其设置为测试。

[TestInitialize]
public void TestInitialize() {
    _userId = "1";
    var data = new List<Contact>();//To store test data.
    //Configure repository
    var fakeRepository = new FakeContactRepository(data);
    //Configure UoW
    _mockUoW = new Mock<IUnitOfWork>();
    _mockUoW.SetupGet(u => u.Contacts).Returns(fakeRepository );

    _controller = new ContactsController(_mockUoW.Object);

    _controller.MockCurrentUser(_userId, "user@domain.com");
}