开发者

Issue with Mock repository

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 InstanceVersi开发者_StackOverflow社区on(), 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜