为TDD存储库模拟DbContext [英] Mocking DbContext for TDD Repository

查看:50
本文介绍了为TDD存储库模拟DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试模拟耦合到我的存储库的EF上下文. 我正在使用Moq,尝试设置模拟的Context并由构造函数将其传递到存储库中.

Trying to Mock my EF Context which is coupled to my Repository. Im using Moq, trying to setup a mocked Context and pass it into the Repository by the constructor.

之后,我调用Add方法,以简单地添加一个新对象,然后我通过检查传入的上下文是否已更改状态来尝试声明...

After that Im calling the Add method, to simply add a new object which I after that try to Assert by checking if the context i passed in has changed state...

错误即时消息是NullReference异常,我猜是因为我的模拟不正确.

Error Im getting is a NullReference Exception and I guess its because my mocking isn't correct..

这是代码:

无法正常运行的测试

[TestClass]
public class GameRepositoryTests
{
    [TestMethod]
    public void PlayerThatWonMustBeAddedToTopList()
    {
        // Arrange
        var expected = "Player added successfully";

        var dbContextMock = new Mock<Context>();
        // Need to setup the Context??

        IRepository gameRepository = new GameRepository(dbContextMock.Object);

        var user = "MyName";

        // Act
        gameRepository.Add(user);

        // Assert

        dbContextMock.VerifySet(o => o.Entry(new ScoreBoard()).State = EntityState.Added);
    }
}

public class ScoreBoard
{

}

存储库

public class GameRepository : IRepository
{
    private readonly Context _context;

    public GameRepository()
        : this(new Context())
    {
        // Blank!
    }

    // Passing in the Mock here...
    public GameRepository(Context context)
    {
        this._context = context;
    }

    // Method under test...
    public void Add<T>(T entity) where T : class
    {
        _context.Set<T>().Add(entity);
    }
}

上下文

public class Context : DbContext
{
    public Context()
        : base("name=DefaultConnection")
    {

    }
}

推荐答案

您需要模拟Set<T>()调用.

类似的事情应该可以解决.

Something like this should work out.

// Arrange
var context = new Mock<Context>();
var set = new Mock<DbSet<User>>();

context.Setup(c => c.Set<User>()).Returns(set.Object);

// Act

// Assert
set.Verify(s => s.Add(It.IsAny<User>()), Times.Once());

除了在基础DbSet上调用了Add()之外,您实际上不需要进行任何验证.无需对Entity状态已被修改的事实进行验证.如果您确认调用了Add(),就足够了,因为您可以放心地假设EF正常工作.

You don't really need to make verify anything except that Add() was called on the underlying DbSet. Doing your verify on the fact that the Entity state was modified is unnecessary. If you verify that Add() was called that should be enough as you can safely assume that EF is working properly.

此示例仅适用于您的User对象的存储库.您必须以这种方式为要测试的每个存储库设置不同的模拟.如果需要,您可能会编写一个更通用的版本.

This example only works for repositories for your User object. You would have to setup the mocks differently for each repository you want to test in this way. You could probably write up a more generic version of this if needed.

这篇关于为TDD存储库模拟DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆