我试图用 asp.net mvc3 和单元测试来弄湿我的脚。
我创建了一个使用存储库模式的模型。这是界面:
public interface IExtensionRepository
{
IList<Extensions> ListAll();
}
这是存储库:
public class ExtensionRepository : IExtensionRepository
{
private ExtensionsLSDataContext _dataContext;
public ExtensionRepository()
{
_dataContext = new ExtensionsLSDataContext();
}
public IList<Extensions> ListAll()
{
var extensions = from ext in _dataContext.Extensions
select ext;
return extensions.ToList();
}
}
这是控制器:
public class ExtensionController : Controller
{
private IExtensionRepository _repository;
public ExtensionController()
: this(new ExtensionRepository())
{
}
public ExtensionController(IExtensionRepository repository)
{
_repository = repository;
}
}
这些页面似乎按设计运行。然而,我的单元测试却误入歧途。它位于同一解决方案中的另一个项目中。我正在使用起订量和 NUnit。这是我的测试:
[Test]
public void Test_Extension_Index_Views()
{
Mock<Extensions> extension = new Mock<Extensions>();
List<Extensions> extList = new List<Extensions>();
extension.Object.Extension = "5307";
extension.Object.Extension_ID = 1;
extension.Object.ExtensionGroup_ID = 1;
extList.Add(extension.Object);
Mock<IExtensionRepository> repos = new Mock<IExtensionRepository>();
repos.Setup(er => er.ListAll()).Returns(extList);
var controller = new ExtensionController(repos);
var result = controller.Index() as ViewResult;
Assert.AreEqual("Index", result.ViewName);
}
对于以“var controller ...”开头的行,我收到以下错误:
'MvcApplication1.Controllers.ExtensionController.ExtensionController(MvcApplication1.Models.IExtensionRepository)' 的最佳重载方法匹配有一些无效参数
和:
参数 1:无法从 'Moq.Mock' 转换为 'MvcApplication1.Models.IExtensionRepository'
我知道我在某个地方错过了船,但我不知道在哪里……有什么想法吗?