MOQ创建一个模拟对象,接受8个参数并返回一个布尔值

时间:2018-05-23 10:03:48

标签: c# moq

我正在尝试为接口INotificationService创建一个模拟对象并进行设置,以便方法PushNotification(接受8个参数)将始终返回true或false。

以下似乎无法按预期工作。

我是否也需要使用Callback方法来执行此操作?

        var notificationServiceMock = new Mock<INotificationService>();
        notificationServiceMock
            .Setup(n => n.PushNotification(
                It.IsAny<long>(), 
                It.IsAny<Guid>(), 
                It.IsAny<string>(), 
                It.IsAny<string>(), 
                It.IsAny<DateTime>(), 
                It.IsAny<string>(), 
                It.IsAny<decimal>(), 
                It.IsAny<string>()))
            .Returns<bool>(b => { return true; });

1 个答案:

答案 0 :(得分:1)

var notificationServiceMock = new Mock<INotificationService>();

notificationServiceMock.Setup(n => n.PushNotification(
            It.IsAny<long>(), 
            It.IsAny<Guid>(), 
            It.IsAny<string>(), 
            It.IsAny<string>(), 
            It.IsAny<DateTime>(), 
            It.IsAny<string>(), 
            It.IsAny<decimal>(), 
            It.IsAny<string>()))
        .Returns(true);

INotificationService ntfObj = notificationServiceMock.Object;