0

我解决了我的问题,但不确定为什么事情会正常工作并且我会问,因为我总是想知道为什么事情会以他们的方式工作。

好的,我正在开发一个 MVC 3 应用程序,它使用通用存储库、用于 DI 的统一和用于单元测试的起订量。在我编辑项目的单元测试中,我的控制器最初具有以下内容: 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

4

1 回答 1

1

你得到 null 因为你从来没有设置你CountyService在调用时返回正确的数据GetById。添加下面的代码来设置模拟,你会很好地使用这个GetById方法:

mock.Setup(m => m.GetById(1).Returns(new State { Id = 1, ... });

或将其设置为与传递给方法的任何参数一起使用:

mock.Setup(m => m.GetById(It.IsAny<int>()).Returns(new State { ... });
于 2012-04-22T17:48:45.277 回答