模拟存储库的问题

发布于 2024-12-01 13:06:47 字数 3265 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖微凉心微凉 2024-12-08 13:06:47

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

您已经设置:

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);

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

The expression that you are using does not match that which you have setup.

You have setup:

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

Whereas, you need to do it as:

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

Moq will analyse the expression to determine when you do:

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

It will check the inputs against the expected inputs.

就此别过 2024-12-08 13:06:47

经过与同事进一步讨论,这似乎是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文