gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:12:21 27 4
gpt4 key购买 nike

我是 Moq 的新手,不太清楚为什么它不能运行。

存储库界面

using System.Collections.Generic;

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

服务接口(interface)

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

最佳答案

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

[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(最新的稳定版本)对我来说一直坚如磐石。

关于unit-testing - 如何使用 Moq 设置简单的单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3114948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com