Reputation:
I'm using Moq and NUnit to create some tests for a Windows service. I have the following:
mockPushService = Mock.Of<IPushService>(m =>
m.SendPushNotification(It.IsAny<UserDevice>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<string>())); // Breaks.
mockMailService = Mock.Of<IEmailService>(m =>
m.SendMessage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()) &&
m.ThrowErrors == true);
mockMailService
is working fine with the LINQ syntax because the methods aren't void. However, the mockPushService
isn't working because it's a void method. I know I'm supposed to use Verifiable
, but I'm not sure how to fit it in using this syntax.
To be more clear, I get this compile-time error:
Cannot convert lambda expression to delegate type 'System.Func<SurgeHub.Domain.Services.IPushService,bool>' because some of the return types in the block are not implicitly convertible to the delegate return type
Cannot implicitly convert type 'void' to 'bool'
How can I make this work? Thank you.
Upvotes: 1
Views: 647
Reputation: 3
You can't use that because the overload expects your method to return bool always. I dont understand a lot of the Of(T) method, but i guess you should use setup normaly.
Upvotes: 0
Reputation: 3034
I've always used the Moq Setup method. So your first example would become;
var push = new Mock<IPushService>();
push.Setup(a => a.SendPushNotification(It.IsAny<UserDevice>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<string>()));
Likewise your second example, which is working anyway, would be as follows (notice the returns which I assume is a bool)
var mail = new Mock<IEmailService>();
mail.Setup(a => a.SendMessage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(true);
mail.SetupProperty<bool>(a => a.ThrowErrors, true);
Upvotes: 3