模拟存储库的问题
我有一个包含以下方法的服务,它查询存储库,如下所示:
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)))))
似乎表达式我传入的测试装置中存储库设置的一部分与服务本身定义的表达式不匹配。随后,最后的断言不成立,因为它“期望一个包含 4 个项目的列表,但实际上返回一个包含 0 个项目的列表”
有人知道可能会出现什么问题吗?
I have a service containing the following method, which queries a repository, like so:
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));
}
I am trying to write a unit test to mock this call, like so:
[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);
}
but when I run it, I get the following error:
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)))))
It seems that the expression I pass in as part of the repository setup in my test fixture is not matching the expression defined in the service itself. Subsequently, the Assert at the end does not hold true, as it "expects a list with 4 items, but actually returns a list of 0 items"
Does anyone have any idea what could be going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的表达式与您设置的表达式不匹配。
您已经设置:
然而,您需要这样做:
Moq 将分析表达式以确定您何时执行:
它将根据预期输入检查输入。
The expression that you are using does not match that which you have setup.
You have setup:
Whereas, you need to do it as:
Moq will analyse the expression to determine when you do:
It will check the inputs against the expected inputs.
经过与同事进一步讨论,这似乎是 NHibernate 本身的问题。它与Where 语句中的两个(相同)条件不匹配。
Upon further discussion with colleagues, this appears to be an issue with NHibernate itself. It does not match the two (identical) conditions in the Where statement.