Di seguito è riportato il codice. creare una classe lib aggiungere il riferimento al framework NUnit 2.5.3.9345 e Moq.dll 4.0.0.0 e incollare il seguente codice. Prova in esecuzione sulla mia macchina si gettaPerché Moq sta lanciando "l'Invocazione prevista sulla simulazione almeno una volta". Dove viene impostato una volta, anche se è impostato su null?
TestCase
'MoqTest.TryClassTest.IsMessageNotNull'
fallito: Moq.MockException: Si ritiene
invocazione sul finto almeno una volta,
ma non fu mai eseguito: v => v.Model
= It.Is (valore (Moq.It + <> c__DisplayClass21[MoqTest.GenInfo]).match)
1 finto,
at
Moq.Mock.ThrowVerifyException(IProxyCall
expected, Expression expression, Times
times, Int32 callCount) at
Moq.Mock.VerifyCalls(Interceptor
targetInterceptor, MethodCall
expected, Expression expression, Times
times) at
Moq.Mock.VerifySet[T](Mock
Action1 setterExpression, Times
1.VerifySet (Action`1
times, String failMessage) at
Moq.Mock
setterExpression) Class1.cs (22, 0): a
MoqTest.TryClassTest.IsMessageNotNull()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Moq;
using NUnit.Framework;
namespace MoqTest
{
[TestFixture]
public class TryClassTest
{
[Test]
public void IsMessageNotNull()
{
var mockView = new Mock<IView<GenInfo>>();
mockView.Setup(v => v.ModuleId).Returns("");
TryPresenter tryPresenter = new TryPresenter(mockView.Object);
tryPresenter.SetMessage(new object(), new EventArgs());
// mockView.VerifySet(v => v.Message, Times.AtLeastOnce());
mockView.VerifySet(v => v.Model = It.Is<GenInfo>(x => x != null));
}
}
public class TryPresenter
{
private IView<GenInfo> view;
public TryPresenter(IView<GenInfo> view)
{
this.view = view;
}
public void SetMessage(object sender, EventArgs e)
{
this.view.Model = null;
}
}
public class MyView : IView<GenInfo>
{
#region Implementation of IView<GenInfo>
public string ModuleId
{
get; set;
}
public GenInfo Model
{
get; set;
}
#endregion
}
public interface IView<T>
{
string ModuleId { get; set; }
T Model { get; set; }
}
public class GenInfo
{
public String Message { get; set; }
}
}
E se si cambia una riga
mockView.VerifySet(v => v.Model = It.Is<GenInfo>(x => x != null));
a
mockView.VerifySet(v => v.Model, Times.AtLeastOnce());
funziona benissimo.
Penso che l'eccezione non sia corretta.