Poniżej znajduje się kod. utwórz bibliotekę klasy dodaj wzorzec do NUnit framework 2.5.3.9345 i Moq.dll 4.0.0.0 i wklej następujący kod. Spróbuj uruchomić go na moim komputerze rzucaDlaczego Moq rzuca "oczekiwane wywołanie na próbę co najmniej raz". Gdzie jest ustawiany jeden raz, nawet jeśli jest ustawiony na wartość null?
TESTCASE
'MoqTest.TryClassTest.IsMessageNotNull'
failed: Moq.MockException: oczekuje
wezwanie na mock przynajmniej raz,
ale nigdy nie została wykonana: V => v.Model
= It.Is (wartość (Moq.It + <> c__DisplayClass21[MoqTest.GenInfo]).match)
1 pośmiewanie
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
Działanie1 setterExpression, Times
1.VerifySet (Action`1
times, String failMessage) at
Moq.Mock
setterExpression) Class1.cs (22, 0): co
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; }
}
}
A jeśli zmienisz jedną linię
mockView.VerifySet(v => v.Model = It.Is<GenInfo>(x => x != null));
do
mockView.VerifySet(v => v.Model, Times.AtLeastOnce());
to działa dobrze.
Myślę, że wyjątek jest niepoprawny.