[英]Moq - It.Is parameter being evaluated as null
I'm trying to clean up my unit tests by implementing this pattern .我试图通过实现这个模式来清理我的单元测试。 Unfortunately, I'm having a problem with my setups, if I try to use It.Is in the matching, so:
不幸的是,如果我尝试在匹配中使用 It.Is,我的设置有问题,所以:
public class MockEmployeeExclusionRulesService : Mock<IEmployeeExclusionRulesService>
{
public MockEmployeeExclusionRulesService MockIsEmployeeExcludedFromSubmissionAsync(FpsFileContentEmployee fpsFileContentEmployee, bool output)
{
Setup(x => x.IsEmployeeExcludedFromSubmissionAsync(fpsFileContentEmployee))
.ReturnsAsync(output);
return this;
}
}
_employeeExclusionRulesServiceMock.MockIsEmployeeExcludedFromSubmissionAsync(
It.Is<FpsFileContentEmployee>(y =>
y.FpsId == _fpsId &&
y.PersonOnPayrollId == _personOnPayrollId),
output: excludeFromSubmission);
When I step through the code, I've found that in the Setup method, fpsFileContentEmployee
is null
.当我单步执行代码时,我发现在 Setup 方法中,
fpsFileContentEmployee
是null
。 I've found this answer which I think explains what's going on, but I'm not sure what they mean by "make it an expression typed variable" - what am I making an expression and how do I use it?我找到了这个答案,我认为它可以解释发生了什么,但我不确定“使其成为表达式类型变量”是什么意思——我在制作什么表达式以及如何使用它?
Attempt at a minimal reproducible example:尝试一个最小的可重现示例:
public class InputClass
{
public string Foo {get; set;}
public string Bar {get; set;}
}
public interface IMockClass
{
bool DoTheThing(InputClass inputClass);
}
public class MockMockClass : Mock<IMockClass>
{
public MockMockClass MockDoTheThing(InputClass inputClass, bool output)
{
Setup(x => x.DoTheThing(inputClass))
.Returns(output);
return this;
}
}
[TestFixture]
public class AllTheTests
{
private readonly MockMockClass _mockClassMock = new ();
[Test]
public void DoTheThingTest()
{
_mockClassMock.MockDoTheThing(
It.Is<InputClass>(x =>
x.Foo == "spam"),
output: true);
}
}
I believe that if you debug the test and step through line by line, you'll find that when it gets into MockMockClass.MockDoTheThing()
, inputClass
will be null
.我相信如果你调试测试并逐行执行,你会发现当它进入
MockMockClass.MockDoTheThing()
时, inputClass
将是null
。
EDIT: It looks like the problem is trying to assign It.is
to a variable - the situation is described further here .编辑:看起来问题是试图将
It.is
分配给一个变量 - 这种情况在此处进一步描述。 Unfortunately, with the pattern I'm using that's fairly unavoidable, so I need to figure out a workaround.不幸的是,我使用的模式是不可避免的,所以我需要找出一个解决方法。
I've managed to find a workaround by creating an overload that takes an expression, like this:我设法通过创建带有表达式的重载来找到解决方法,如下所示:
public class MockMockClass : Mock<IMockClass>
{
public MockMockClass MockDoTheThing(InputClass inputClass, bool output)
{
Setup(x => x.DoTheThing(inputClass))
.Returns(output);
return this;
}
public MockMockClass MockDoTheThing(Expression<Func<InputClass, bool>> inputClassMatch, bool output)
{
Setup(x => x.DoTheThing(It.Is(inputClassMatch)))
.Returns(output);
return this;
}
}
[TestFixture]
public class AllTheTests
{
private readonly MockMockClass _mockClassMock = new ();
[Test]
public void DoTheThingTest()
{
_mockClassMock.MockDoTheThing(x =>
x.Foo == "spam",
output: true);
}
}
It's not my favourite solution, but it seems to work.这不是我最喜欢的解决方案,但它似乎有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.