我解决了我的问题,但我不确定为什么事情正在发挥作用并且我在问,因为我总是想知道为什么事情会按照他们的方式运作。
好的,我正在开发一个MVC 3应用程序,它使用通用存储库,统一用于DI,以及moq用于单元测试。在我编辑项目的单元测试中,我的控制器最初具有以下内容: AnEntity obj = _anEntityService.GetById(id);
但是,当我的单元测试调用控制器中的Edit(1)时,返回null。当我改变阅读时 AnEntity obj = _anEntityService.GetAll()。FirstOrDefault(p => p.Id == id);
它正确地返回了模拟项目。
现在,我很高兴我弄清楚了,然而,我仍然在摸索为什么它适用于单元测试,但GetById在我运行应用程序时正在工作。
任何见解都是最有帮助的。
提前致谢。
以下是模拟数据:
public class AdministrationMockData
{
#region Administration Mock Data
#region County Mock Data
public static void CreateCounty(ref Mock<ICountyService> mock)
{
mock.Setup(m => m.GetAll()).Returns(new List<County>
{
new County { Id = 1, Name = "Adams", StateId = 39, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 2, Name = "Berks", StateId = 39, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 3, Name = "Chester", StateId = 39, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 4, Name = "York", StateId = 39, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 5, Name = "Baltimore", StateId = 21, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 6, Name = "Montgomery", StateId = 21, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" }
}.AsQueryable());
}
#endregion County Mock Data
#region State Mock Data
public static void CreateState(ref Mock<IStateService> mock)
{
mock.Setup(m => m.GetAll()).Returns(new List<State>
{
new State { Id = 21, Name = "Maryland", Code = "MD", IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new State { Id = 39, Name = "Pennsylvania", Code = "PA", IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" }
}.AsQueryable());
}
#endregion State Mock Data
#endregion Administration Mock Data
}
以下是编辑的单元测试
[TestMethod]
public void Can_Edit_County()
{
// Arrange
// - create the mock repositories
Mock<ICountyService> mockCounty = new Mock<ICountyService>();
Mock<IStateService> mockState = new Mock<IStateService>();
AdministrationMockData.CreateCounty(ref mockCounty);
AdministrationMockData.CreateState(ref mockState);
// Arrange
// - create a controller and make the page size 6 items
CountyController controller = new CountyController(mockCounty.Object, mockState.Object);
controller.PageSize = 6;
controller.Testing = true;
// Act
County c1 = controller.Edit(1).ViewData.Model as County;
County c2 = controller.Edit(2).ViewData.Model as County;
County c3 = controller.Edit(3).ViewData.Model as County;
// Assert
Assert.AreEqual(1, c1.Id);
Assert.AreEqual(2, c2.Id);
Assert.AreEqual(3, c3.Id);
}
这是控制器编辑例程:
public ViewResult Edit(int id)
{
//County obj = _countyService.GetById(id);
County obj = _countyService.GetAll().FirstOrDefault(p => p.Id == id);
if (!Testing)
{
PopulateCountyDropDownLists(obj.StateId);
}
return View(obj);
}
我有一个通用的存储库和接口存储库,它由TEntity为每个特定实体实例化,并且还有一个服务和接口服务来执行存储库。我的存储库和服务基于:http://efmvc.codeplex.com/releases/view/58663
答案 0 :(得分:1)
您获得null,因为您在调用CountyService
时从未设置GetById
以返回正确的数据。添加以下代码来设置模拟,您最好使用GetById
方法:
mock.Setup(m => m.GetById(1).Returns(new State { Id = 1, ... });
或将其设置为与传递给mothod的任何参数一起使用:
mock.Setup(m => m.GetById(It.IsAny<int>()).Returns(new State { ... });