Mock存储库问题

时间:2011-08-25 15:51:10

标签: c# linq mocking moq

我有一个包含以下方法的服务,它查询存储库,如下所示:

public IEnumerable<Variable> ListVariables(int instanceId, int instanceVersionId, TypeGroup typeGroup)
{
    return
        _variableRepository.Where(x => x.InstanceVersion.Instance.Id == instanceId && x.InstanceVersion.Version == instanceVersionId && x.VariableType.VariableTypeGroup.Id == Convert.ToInt32(typeGroup));
}   

我正在尝试编写一个单元测试来模拟这个调用,如下所示:

[Test]
        public void ListVariables_Returns_Variables_From_Repository()
        {
            IEnumerable<Variable> reposVariables = new List<Variable>
                                                    {
                                                        new Variable {InstanceVersion = new InstanceVersion(), VariableType = new VariableType(), Value = "test value1"},
                                                        new Variable {InstanceVersion = new InstanceVersion(), VariableType = new VariableType(), Value = "test value2"},
                                                        new Variable {InstanceVersion = new InstanceVersion(), VariableType = new VariableType(), Value = "test value3"},
                                                        new Variable {InstanceVersion = new InstanceVersion(), VariableType = new VariableType(), Value = "test value4"}
                                                    };

            var mockVariableRepository = new Mock<IVariableRepository>();
            mockVariableRepository.Setup(y => y.Where(x => x.InstanceVersion.Instance.Id == 1 && x.InstanceVersion.Version == 1 && x.VariableType.VariableTypeGroup.Id == 1)).Returns(reposVariables).Verifiable();

            var service = CreateSpiralService(variableRepository: mockVariableRepository.Object);

            var result = service.ListVariables(1,1,TypeGroup.Information).ToList<Variable>();

            mockVariableRepository.Verify(y => y.Where(x => x.InstanceVersion.Instance.Id == 1 && x.InstanceVersion.Version == 1 && x.VariableType.VariableTypeGroup.Id == 1), Times.Once()); ;
            Assert.AreEqual(reposVariables, result);
        }

但是当我运行它时,我收到以下错误:

Moq.MockException : 
Expected invocation on the mock once, but was 0 times: y => y.Where(x => (x.InstanceVersion.Instance.Id == 1 && x.InstanceVersion.Version == 1) && x.VariableType.VariableTypeGroup.Id == 1)

Configured setups:
y => y.Where(x => (x.InstanceVersion.Instance.Id == 1 && x.InstanceVersion.Version == 1) && x.VariableType.VariableTypeGroup.Id == 1), Times.Never

Performed invocations:
IReadOnlyNoIdRepository`1.Where(x => (((x.InstanceVersion.Instance.Id == value(Core.Services.Spiral.SpiralService+<>c__DisplayClass8).instanceId) AndAlso (x.InstanceVersion.Version == value(Core.Services.Spiral.SpiralService+<>c__DisplayClass8).instanceVersionId)) AndAlso (x.VariableType.VariableTypeGroup.Id == ToInt32(Convert(value(Core.Services.Spiral.SpiralService+<>c__DisplayClass8).typeGroup)))))

似乎我在测试夹具中作为存储库设置的一部分传入的表达式与服务本身中定义的表达式不匹配。随后,最后的Assert不成立,因为它“期望一个包含4个项目的列表,但实际上返回0个项目的列表”

有没有人知道会出现什么问题?

2 个答案:

答案 0 :(得分:1)

您使用的表达式与您设置的表达式不匹配。

您已设置:

y => y.Where(x => (x.InstanceVersion.Instance.Id == 1 && x.InstanceVersion.Version == 1) && x.VariableType.VariableTypeGroup.Id == 1)

然而,你需要这样做:

m => m.ListVariables(
  It.Is<int>(i ==> i == 1), 
  It.Is<int>(i ==> i == 1), 
  It.Is<TypeGroup>(t => t == TypeGroup.Information))

Moq将分析表达式以确定何时执行:

var service.ListVariables(1, 1, TypeGroup.Information);

它将根据预期输入检查输入。

答案 1 :(得分:0)

经过与同事的进一步讨论,这似乎是NHibernate本身的一个问题。它与Where语句中的两个(相同)条件不匹配。