C# 当设置返回虚拟数据时,Moq调用返回空

C# 当设置返回虚拟数据时,Moq调用返回空,c#,unit-testing,nunit,ninject,moq,C#,Unit Testing,Nunit,Ninject,Moq,当指定要从模拟存储库返回的模拟数据时,我挂断了。我的测试方法: [TestMethod] public void GetAllImports_SomeImportRecordsExist_ReturnsNonEmptyList() { // Arrange var repo = new Mock<IImportRepository>(); repo.Setup(import => import.GetAll()).Returns(new[] {new I

当指定要从模拟存储库返回的模拟数据时,我挂断了。我的测试方法:

[TestMethod]
public void GetAllImports_SomeImportRecordsExist_ReturnsNonEmptyList() {
    // Arrange
    var repo = new Mock<IImportRepository>();
    repo.Setup(import => import.GetAll()).Returns(new[] {new Import()});

    var manager = new ImportConfigurationManager(repo.Object);

    // Act
    var result = manager.GetAllImports();

    // Assert
    Assert.IsNotNull(result);
    Assert.IsInstanceOfType(result, typeof (IList<Import>));
    Assert.IsTrue(result.Any());
}
[TestMethod]
public void GetAllImports\u SomeImportRecordsExist\u ReturnsNonptyList(){
//安排
var repo=new Mock();
repo.Setup(import=>import.GetAll()).Returns(new[]{new import()});
var manager=新的导入配置管理器(repo.Object);
//表演
var result=manager.GetAllImports();
//断言
Assert.IsNotNull(结果);
IsInstanceOfType(result,typeof(IList));
Assert.IsTrue(result.Any());
}
最后一个断言失败,因为被测试的管理器返回一个空列表。经理:

public class ImportConfigurationManager : IImportConfigurationManager
{
    private readonly IImportRepository _importRepository;

    public ImportConfigurationManager(IImportRepository repository)
    {
        _importRepository = repository;
    }

    public IList<Import> GetAllImports() {
        return _importRepository.GetAll(import => import) ?? new Import[0];
    }
}
公共类导入配置管理器:IImportConfiguration管理器
{
专用只读IImportRepository\u导入存储库;
公共导入配置管理器(IImportRepository存储库)
{
_importRepository=存储库;
}
公共IList GetAllImports(){
return _importRepository.GetAll(import=>import)??新导入[0];
}
}
我已经完成了测试,并观察了管理器对GetAll的调用返回null,因此我认为我的错误在于设置模拟存储库实例。任何帮助都将不胜感激

更新


Patrick指出,在我的经理和测试中,我给GetAll打的电话是不同的。使呼叫一致(在任何方向)可以解决问题。谢谢。

以下内容可能会有所帮助。我得到一份进口清单。在设置中返回列表,并确保Func的设置正确

    public class ImportConfigurationManager : IImportConfigurationManager
    {
        private readonly IImportRepository _importRepository;

        public ImportConfigurationManager(IImportRepository repository)
        {
            _importRepository = repository;
        }

        public IList<Import> GetAllImports()
        {
            var imports = _importRepository.GetAll(import => import) ?? new Import[0];
            return imports;
        }
    }

    public interface IImportRepository
    {
        IList<Import> GetAll(Func<object, object> func);
    }

    public interface IImportConfigurationManager
    {
    }

    [TestMethod]
    public void GetAllImports_SomeImportRecordsExist_ReturnsNonEmptyList()
    {
        // Arrange
        var repo = new Mock<IImportRepository>();
        repo.Setup(import => import.GetAll(It.IsAny<Func<object, object>>())).Returns(new List<Import>(){new Import()});

        var manager = new ImportConfigurationManager(repo.Object);

        // Act
        var result = manager.GetAllImports();

        // Assert
        Assert.IsNotNull(result);
        Assert.IsInstanceOfType(result, typeof(IList<Import>));
        Assert.IsTrue(result.Any());
    }
公共类导入配置管理器:IImportConfiguration管理器
{
专用只读IImportRepository\u导入存储库;
公共导入配置管理器(IImportRepository存储库)
{
_importRepository=存储库;
}
公共IList GetAllImports()
{
var imports=_importRepository.GetAll(import=>import)??新导入[0];
退货进口;
}
}
公共接口IImportRepository
{
IList GetAll(Func-Func);
}
公共接口IImportConfiguration Manager
{
}
[测试方法]
public void GetAllImports\u SomeImportRecordsExist\u ReturnsNonptyList()
{
//安排
var repo=new Mock();
repo.Setup(import=>import.GetAll(It.IsAny()).Returns(newlist(){newimport()});
var manager=新的导入配置管理器(repo.Object);
//表演
var result=manager.GetAllImports();
//断言
Assert.IsNotNull(结果);
IsInstanceOfType(result,typeof(IList));
Assert.IsTrue(result.Any());
}

IImportRepository.GetAll()有两个版本,一个不带参数(在模拟设置中引用),另一个带lambda(在管理器中使用)。这就是问题所在吗?@PatrickQuirk-Nice-catch,这就解决了问题。@spock,在做了更多的测试之后,当我的测试与参数无关时,我现在也使用了'Is-any'样式的参数。帕特里克确实抓住了我的问题,但我同意你的设置,标记为答案。谢谢