Running the unit tests against my latest build I got an unexpected failure. It was odd because the test was against code I hadn't changed. The test looks something like this:
[Fact]
public void My_Test()
{
// Arrange
var expectedValuation = new Money { Amount = 0, Currency = "GBP" };
this.mockMoneyConverter.Setup(x => x.Convert(It.IsAny<MoneyWorks.Money>(), It.IsAny<Money>(), It.IsAny<ResolutionContext>()))
.Returns(expectedValuation);
var input = new QueryResults.ExistingItemResult
{
Balances = null
};
// Act
var actual = this.target.Map<ExistingItemSummary>(input);
// Assert
actual.Valuation.ShouldBe(expectedValuation);
this.mockMoneyConverter.Verify(x => x.Convert(It.Is<MoneyWorks.Money>(money => money.Amount == 0), It.IsAny<Money>(), It.IsAny<ResolutionContext>()));
}
The ShouldBe passes fine but the Verify throws an error:
Message:
System.NullReferenceException : Object reference not set to an instance of an object.
lambda_method(Closure , Money )
<>c__DisplayClass2_0`1.<Is>b__0(TValue value)
Match`1.Matches(Object value)
Matcher.Matches(Object value)
MethodCall.Matches(ICallContext call)
<>c__DisplayClass56_0.<VerifyCalls>b__0(ICallContext ac)
WhereListIterator`1.MoveNext()
Enumerable.Count[TSource](IEnumerable`1 source)
Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
Mock.Verify[T,TResult](Mock`1 mock, Expression`1 expression, Times times, String failMessage)
Mock`1.Verify[TResult](Expression`1 expression)
What I find peculiar is that if I replace the Verify with a genric It.IsAny check, it works and passes:
this.mockMoneyConverter.Verify(x => x.Convert(It.IsAny<MoneyWorks.Money>(), It.IsAny<Money>(), It.IsAny<ResolutionContext>()));
Is there any reason why the original It.Is check should start to fail all of a sudden?
money.Amount == 0
If money is null, it won't be able to evaluate this and I'd expect a null reference exception.
Changing it to handle the null case should sort it out if that's your issue
It.Is<MoneyWorks.Money>(money => money != null && money.Amount == 0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.