2014-03-03 60 views
1

我正在使用Moq和NUnit爲Windows服務創建一些測試。我有以下幾點:Moq使用LINQ語法驗證

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,因爲這些方法並不作廢工作正常與LINQ語法。然而,mockPushService不工作,因爲它是一個無效的方法。我知道我應該使用Verifiable,但我不確定如何使用這種語法來適應它。

更清楚,我得到這個編譯時錯誤:

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' 

我怎樣才能使這項工作?謝謝。

+0

你能詳細解釋一下「休息」嗎?怎麼了?應該發生什麼? –

回答

3

我一直使用Moq設置方法。所以你的第一個例子會成爲;

var push = new Mock<IPushService>(); 
push.Setup(a => a.SendPushNotification(It.IsAny<UserDevice>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<string>())); 

同樣你的第二個例子,這是工作,無論如何,將如下所示(請注意,我認爲是一個布爾值的回報)

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); 
0

不能使用,由於超載期待您的方法總是返回bool。我不明白很多的(T)方法,但我想你應該使用安裝normaly。