模拟方法 IQueryable<T> FindBy(Expression<Func<T, bool>> expression);
Mocking method IQueryable<T> FindBy(Expression<Func<T, bool>> expression);
我想用 Moq 模拟这个界面:
IQueryable<T> FindBy(Expression<Func<T, bool>> expression);
下面是调用方法的代码,想mock
string notificationMedium = await _notificationTypeRepository
.FindBy(x => x.Alias.ToLower().Equals(request.NotificationType.ToLower())).Select(x => x.NotificationMedium).FirstOrDefaultAsync();
我正在考虑做类似
的事情
repository.Setup(x => x.FindBy(It.IsAny<Expression<Func<NotificationType, bool>>>()))
.Returns((Func<Expression<Func<NotificationType, bool>>, IQueryable<NotificationType>>)(expression => list.AsQueryable().Where(expression)));
当我尝试在需要此模拟的地方调试单元测试时,它说
ValidateCreateNotificationCommandHandler
Duration: 8.3 sec
Message:
TestBase.Assertion`1[System.Collections.Generic.IEnumerable`1[System.String]] : Failed :
Should not contain x => x.Matches("Error", RegexOptions.None)
Actual :
----------------------------
----------------------------
Asserted : actual => !a.Any(predicate.Compile())
predicate → x => x.Matches("Error", RegexOptions.None)
predicate.Compile() → System.Func`2[System.String,System.Boolean]
Stack Trace:
Assert.That[T](T actual, Expression`1 predicate, String comment, Object[] commentArgs)
IEnumerableShoulds.ShouldNotContain[T](IEnumerable`1 actual, Expression`1 predicate, String comment, Object[] args)
CreateNotificationCommandHandlersTester.ValidateCreateNotificationCommandHandler() line 84
GenericAdapter`1.GetResult()
AsyncToSyncAdapter.Await(Func`1 invoke)
TestMethodCommand.RunTestMethod(TestExecutionContext context)
TestMethodCommand.Execute(TestExecutionContext context)
<>c__DisplayClass1_0.<Execute>b__0()
BeforeAndAfterTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)
添加命名空间 MockQueryable-
using MockQueryable.Moq;
更改代码-
Mock<IQueryable<NotificationType>> mock = list.AsQueryable().BuildMock();
repository.Setup(x => x.FindBy(It.IsAny<Expression<Func<NotificationType, bool>>>())).Returns(mock.Object);
代码可以工作!
我想用 Moq 模拟这个界面:
IQueryable<T> FindBy(Expression<Func<T, bool>> expression);
下面是调用方法的代码,想mock
string notificationMedium = await _notificationTypeRepository
.FindBy(x => x.Alias.ToLower().Equals(request.NotificationType.ToLower())).Select(x => x.NotificationMedium).FirstOrDefaultAsync();
我正在考虑做类似
的事情repository.Setup(x => x.FindBy(It.IsAny<Expression<Func<NotificationType, bool>>>()))
.Returns((Func<Expression<Func<NotificationType, bool>>, IQueryable<NotificationType>>)(expression => list.AsQueryable().Where(expression)));
当我尝试在需要此模拟的地方调试单元测试时,它说
ValidateCreateNotificationCommandHandler
Duration: 8.3 sec
Message:
TestBase.Assertion`1[System.Collections.Generic.IEnumerable`1[System.String]] : Failed :
Should not contain x => x.Matches("Error", RegexOptions.None)
Actual :
----------------------------
----------------------------
Asserted : actual => !a.Any(predicate.Compile())
predicate → x => x.Matches("Error", RegexOptions.None)
predicate.Compile() → System.Func`2[System.String,System.Boolean]
Stack Trace:
Assert.That[T](T actual, Expression`1 predicate, String comment, Object[] commentArgs)
IEnumerableShoulds.ShouldNotContain[T](IEnumerable`1 actual, Expression`1 predicate, String comment, Object[] args)
CreateNotificationCommandHandlersTester.ValidateCreateNotificationCommandHandler() line 84
GenericAdapter`1.GetResult()
AsyncToSyncAdapter.Await(Func`1 invoke)
TestMethodCommand.RunTestMethod(TestExecutionContext context)
TestMethodCommand.Execute(TestExecutionContext context)
<>c__DisplayClass1_0.<Execute>b__0()
BeforeAndAfterTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)
添加命名空间 MockQueryable-
using MockQueryable.Moq;
更改代码-
Mock<IQueryable<NotificationType>> mock = list.AsQueryable().BuildMock();
repository.Setup(x => x.FindBy(It.IsAny<Expression<Func<NotificationType, bool>>>())).Returns(mock.Object);
代码可以工作!