如何使用Moq设置简单的单元测试? [英] How to setup a simple Unit Test with Moq?

查看:76
本文介绍了如何使用Moq设置简单的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Moq的新手,不太确定为什么它不会运行.

I'm new to Moq and not quite sure why this won't run.

存储库界面

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调用失败.任何帮助将不胜感激!

I wanted to set this up as a very simple unit test. It is failing on the _mockRepository.Setup call. Any help would be appreciated!

编辑-

错误消息

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

推荐答案

与所遇到的问题无关,您可以将模拟初始化移到通用的TestInitialize方法中,该方法将在每次测试之前运行.这样,您可以将通用的初始化代码放在一个地方,并使您的测试更小,更易读.

Unrelated to the problem you are having, you can move the mock initialization out into a common TestInitialize method that would get run before every test. This way you can keep the common init code in one place and make your tests much smaller and more readable.

[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,我需要在此发行版中修复特定的错误,否则3.1.4(最新的稳定发行版)对我来说是坚如磐石.

I've just run this test exactly as posted and it worked for me. I am using Moq v4.0 beta There was a specific bug fix in this release that I needed, otherwise 3.1.4(the latest stable release) has been rock solid for me.

这篇关于如何使用Moq设置简单的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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