【问题标题】:Mock service that takes unitOfWork in constructor在构造函数中采用 unitOfWork 的模拟服务
【发布时间】:2015-06-09 14:47:17
【问题描述】:

我尝试在我的业务逻辑上编写单元测试。

我现在拥有的:

private Mock<IRepository<Theme>> _mockRepository;
    private IBaseService<Theme> _service;
    private Mock<IAdminDataContext> _mockDataContext;
    private List<Theme> _listTheme;

    [TestInitialize]
    public void Initialize()
    {
        _mockRepository = new Mock<IRepository<Theme>>();
        _mockDataContext = new Mock<IAdminDataContext>();
        _service = new ThemeService(_mockDataContext.Object);
        _listTheme = new List<Theme>
                     {
                         new Theme
                         {
                             Id = 1,
                             BgColor = "red",
                             BgImage = "/images/bg1.png",
                             PrimaryColor = "white"
                         },
                         new Theme
                         {
                             Id = 2,
                             BgColor = "green",
                             BgImage = "/images/bg2.png",
                             PrimaryColor = "white"
                         },
                         new Theme
                         {
                             Id = 3,
                             BgColor = "blue",
                             BgImage = "/images/bg3.png",
                             PrimaryColor = "white"
                         }
                     };
    }

    [TestMethod]
    public async Task ThemeGetAll()
    {
        //Arrange
        _mockRepository.Setup(x => x.GetAll()).ReturnsAsync(_listTheme);

        //Act
        List<Theme> results = await _service.GetAll();

        //Assert
        Assert.IsNotNull(results);
        Assert.AreEqual(_listTheme.Count, results.Count);
    }

问题 - 在服务 GetAll 我得到异常,因为对象为空。对象 - 这是存储库。以下是代码详细信息:

public class BaseService<T> : DomainBaseService, IBaseService<T> where T : BaseEntity
    {
        private readonly IAdminDataContext _dataContext;
        private readonly IRepository<T> _repository;

        public BaseService(IAdminDataContext dataContext)
            : base(dataContext)
        {
            this._dataContext = dataContext;
            this._repository = dataContext.Repository<T>();
        }

        public async Task<List<T>> GetAll()
        {
            return await _repository.GetAll();
        }
    }

如您所见,在服务中,我尝试从 unitOfWork (AdminDataContext) 获取存储库。但它始终为空。

我应该如何模拟我的服务来测试它的功能?

【问题讨论】:

    标签: c# unit-testing repository moq business-logic


    【解决方案1】:

    您永远不会设置 Repository 方法来返回您的模拟存储库。只需将以下内容添加到测试的“排列”部分:

    _mockDataContext.Setup(x => x.Repository<Theme>()).Returns(_mockRepository.Object);
    

    【讨论】:

    • 我将此代码添加到Initialize 方法` _mockDataContext.Setup(x => x.Repository()).Returns((Repository) _mockRepository.Object);` 但是获取异常System.InvalidCastException: Unable to cast object of type 'Castle.Proxies.IRepository1Proxy' 以键入'Project.DataAccess.Repository.Repository1[Project.DataAccess.Entities.Theme]'..
    • 但是在我将 unitOfWork 中的返回类型从 Repository&lt;T&gt; 更改为 IRepository&lt;T&gt; 在方法 dataContext.Repository&lt;Theme&gt;() 中之后,它似乎有效。我做了好事吗?)谢谢
    • 是的,你应该返回接口,而不是具体的。如您所见,这样做可以让您在测试期间返回一个模拟对象,在运行时返回一个真实对象。
    猜你喜欢
    • 2020-05-06
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多