如何使用Moq设置简单的单元测试?

时间:2010-06-25 01:14:08

标签: unit-testing moq mocking

我是Moq的新手,并不太清楚为什么不会这样做。

存储库接口

using System.Collections.Generic;

public interface IRepository
{
    IEnumerable<string> list();
}

服务界面

using System.Collections.Generic;

public interface IService
{
    IEnumerable<string> AllItems();
}

服务类

using System.Collections.Generic;

public class Service : IService
{
    private IRepository _repository;

    public Service(IRepository repository)
    {
        this._repository = repository;
    }

    public IEnumerable<string> AllItems()
    {
        return _repository.list();
    }
}

单元测试类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MoqTest;
using MoqTest.Controllers;
using Moq;
using MoqTest.Models;

[TestClass]
public class Tests
{
    private Mock<IRepository> _mockRepository;
    private IService _service;

    [TestMethod]
    public void my_test()
    {
        //Arrange.
        List<string> theList = new List<string>();
        theList.Add("test3");
        theList.Add("test1");
        theList.Add("test2");

        _mockRepository = new Mock<IRepository>();

        //The line below returns a null reference...
        _mockRepository.Setup(s => s.list()).Returns(theList);
        _service = new Service(_mockRepository.Object);

        //Act.
        var myList = _service.AllItems();
        Assert.IsNotNull(myList, "myList is null.");

        //Assert.
        Assert.AreEqual(3, myList.Count());
    }
}

我想把它设置为一个非常简单的单元测试。它在_mockRepository.Setup调用上失败了。任何帮助将不胜感激!

编辑 -

错误消息

Test method Tests.my_test threw exception:  System.NullReferenceException: Object reference not set to an instance of an object..

异常堆栈跟踪

Moq.MethodCall.SetFileInfo()
Moq.MethodCall..ctor(Mock mock, Expression originalExpression, MethodInfo method, Expression[] arguments)
Moq.MethodCallReturn..ctor(Mock mock, Expression originalExpression, MethodInfo method, Expression[] arguments)
ctor(Mock mock, Expression originalExpression, MethodInfo method, Expression[] arguments)
b__11()
Moq.PexProtector.Invoke[T](Func`1 function)
TResult](Mock mock, Expression`1 expression)
Setup[TResult](Expression`1 expression)
Tests.my_test() in C:\Users\xxx\Documents\Visual Studio 2008\Projects\MoqTest\MoqTest.Tests\Controllers\Tests.cs: line 28

1 个答案:

答案 0 :(得分:3)

与您遇到的问题无关,您可以将模拟初始化移动到在每次测试之前运行的公共TestInitialize方法。这样,您就可以将公共init代码保存在一个位置,从而使您的测试更小,更易读。

[TestInitialize]
public void TestInit() {
    //Arrange.
    List<string> theList = new List<string>();
    theList.Add("test3");
    theList.Add("test1");
    theList.Add("test2");

    _mockRepository = new Mock<IRepository>();

    //The line below returns a null reference...
    _mockRepository.Setup(s => s.list()).Returns(theList);
    _service = new Service(_mockRepository.Object);
}

[TestMethod]
public void my_test()
{
    //Act.
    var myList = _service.AllItems();
    Assert.IsNotNull(myList, "myList is null.");

    //Assert.
    Assert.AreEqual(3, myList.Count());
}

我只是按照发布的方式运行此测试,它对我有用。我正在使用Moq v4.0 beta这个版本中有一个特定的bug修复我需要,否则3.1.4(最新的稳定版本)对我来说是坚如磐石的。