我对单元测试很陌生,所以如果这不是一个非常好的问题我会道歉。
我有一个主要的Web项目,以及一个附带的NUnit测试库。我正在使用Unity将接口注入主项目中的控制器。例如:
public class EquipmentController : Controller
{
private readonly ILocationRepository locationContext = null;
private readonly IRepository<EquipmentCategory> categoryContext = null;
private readonly IEquipmentRepository equipmentContext = null;
private readonly IRecordRepository recordContext = null;
public EquipmentController(ILocationRepository locationRepo, IRepository<EquipmentCategory> categoryRepo, IEquipmentRepository equipmentRepo, IRecordRepository recordRepo)
{
this.locationContext = locationRepo;
this.categoryContext = categoryRepo;
this.equipmentContext = equipmentRepo;
this.recordContext = recordRepo;
}
Web应用程序本身实际上按预期工作。但是,我在尝试编写测试用例时遇到了问题。例如,在测试库中,我有以下内容:
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity.Mvc;
using Microsoft.Practices.Unity.ObjectBuilder;
using Microsoft.Practices.Unity.StaticFactory;
using Moq;
using ITEquipmentDatabase;
using ITEquipmentDatabase.Models;
using ITEquipmentDatabase.Controllers;
namespace ITEquipmentDatabase.Tests
{
[TestFixture]
public class EquipmentController
{
[Test]
public void TestMethod()
{
var equipRepo = new Mock<IEquipmentRepository>();
var categoryRepo = new Mock<IRepository<EquipmentCategory>>();
var locationRepo = new Mock<ILocationRepository>();
var recordRepo = new Mock<IRecordRepository>();
EquipmentController controller = new EquipmentController(locationRepo.Object, categoryRepo.Object, equipRepo.Object, recordRepo.Object);
}
}
}
但是,我收到以下错误:
Error 1 'ITEquipmentDatabase.Tests.EquipmentController' does not contain a constructor that takes 4 arguments C:\Users\Khandokar\Documents\Visual Studio 2013\Projects\IT Equipment Log\ITEquipmentDatabase.Tests\EquipmentController.cs 27 46 ITEquipmentDatabase.Tests
我在我的测试项目中引用了Unity,甚至添加了Bootstrapper.cs(不确定是否有必要,但我试图解决上述问题)。我很确定我做错了什么,但我刚开始冒险进行单元测试并且遇到了一些困难时期。
感谢您的任何建议。
答案 0 :(得分:5)
您的测试类名为EquipmentController
,因此您的课程正在测试中。
将您的测试类重命名为EquipmentControllerTests
或其他类似的。
(错误消息是线索;请注意它引用Tests.EquipmentController
)。