9

我正在尝试Moq对 Entity Framework Code First 类进行一些测试。我对 Moq 和模拟技术非常陌生,我想知道是否可以轻松地进行我将在下面描述的测试。我在网上搜索了一些解决方案,但大多数都是基于存储库模式,我想避免这种情况。

我有ITestEntities上下文接口

public interface ITestEntities
{
    IDbSet<Order> Orders { get; }
    IDbSet<Product> Products { get; }
    IDbSet<User> Users { get; }
}

然后上下文

public class TestEntities : DbContext, ITestEntities
{
    public TestEntities() : base("name=TestEntities")
    {

    }

    public virtual IDbSet<Order> Orders { get; set; }
    public virtual IDbSet<Product> Products { get; set; }
    public virtual IDbSet<User> Users { get; set; }
}

一个控制器和一个要测试的动作

public class HomeController : Controller
{
    private ITestEntities db;

    public HomeController()
    {
        db = new TestEntities();
    }

    public HomeController(ITestEntities db)
    {
        this.db = db;
    }

    public ActionResult Index()
    {
        var count = db.Users.Count();
        ViewBag.count = count;

        return View(count);
    }        
}

最后是使用 Moq 的 NUnit 测试

[Test]
public void ModelValueShouldBeTwo()
{
    var mockUsers = new Mock<IDbSet<User>>();
    mockUsers.Setup(m => m.Count()).Returns(2);

    var mockDB = new Mock<ITestEntities>();
    mockDB.Setup(db => db.Users).Returns((IDbSet<User>)mockUsers);

    var controller = new HomeController((ITestEntities)mockDB);

    var view = controller.Index();

    Assert.IsInstanceOf<ViewResult>(view);
    Assert.AreEqual(((ViewResult)view).Model, 2);
}

问题出在这一行:mockUsers.Setup(m => m.Count()).Returns(2);. 运行此测试时,我收到以下错误:

System.NotSupportedException : Expression references a method that does not belong to the mocked object: m => m.Count<User>()

我认为这是由于它.Count()是一种静态方法,因此不能被 Moq 模拟。有没有一种方法可以使用 Moq 而不是使用成熟的存储库模式来测试这个简单的操作,据我所知,无论如何都应该将这.Count()部分硬编码为某种可测试的方法......也许我只是以错误的方式使用模拟?因为我的印象是使用 EF Code First 应该非常简单且可行。

4

2 回答 2

16

如果您正在模拟测试实体,则无需进一步模拟

应该这样做(虽然,我不在 IDE 中,所以可能需要一些调整)

更新以包含新的 InMemoryDbSet

[Test]
public void ModelValueShouldBeTwo()
{
    //Build test users
    var mockUsers = new InMemoryDbSet<User>(){ new User(), new User()};
    var mockDB = new Mock<ITestEntities>();
    //Set up mock entities to returntest users.
    mockDB.Setup(db => db.Users).Returns(mockUsers);

    var controller = new HomeController((ITestEntities)mockDB);

    var view = controller.Index();

    Assert.IsInstanceOf<ViewResult>(view);
    Assert.AreEqual(((ViewResult)view).Model, 2);
}

这意味着扩展方法将简单地使用您提供的测试数据。

See below for a good article on mocking dbset http://geekswithblogs.net/Aligned/archive/2012/12/12/mocking-or-faking-dbset.aspx

于 2013-04-25T12:32:31.377 回答
1

Mock GetEnumerator() instead of Count()

Count() is an extension method on objects that implement IEnumerable<T>, and IDbSet<T> implements IEnumerable<T>

Extension methods are passed the object that they are called on. In this case the signature is:

public static int Count<TSource>(
    this IEnumerable<TSource> source,  //This is your IDbSet that you are mocking
    Func<TSource, bool> predicate
)

Rather than trying to setup Count() to return a specific value, you can setup the members of IEnumerable<T> to achieve the same result. In the case of IEnumerable<T> all you have to do is set up GetEnumerator() to return an Enumerator<T> that enumerates over two values.

In this situation I usually create that Enumerator<T> by creating a new List with a couple of items and calling GetEnumerator() on it:

mockUsers.Setup(m => m.GetEnumerator()).Returns(new List<Users> {
    new User(),
    new User()
}.GetEnumerator());

Now, of course this effectively tests the extension method Count() in addition to whatever you are trying to achieve with your test, while that is a pretty low risk when the extension method is a part of .NET, it is something to keep in mind if you are using and authoring extension methods of your own.

于 2013-04-25T13:30:25.160 回答