2010-04-05 5 views
6

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__DisplayClass2 1[MoqTest.GenInfo]).match)
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
1 finto,
Action 1 setterExpression, Times
times, String failMessage) at
Moq.Mock
1.VerifySet (Action`1
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.

risposta

6

Si utilizza la seguente asserzione VerifySet:

mockView.VerifySet(v => v.Model = It.Is<GenInfo>(x => x != null)); 

che dice sostanzialmente "verificare che ho impostato la proprietà Modello con qualche oggetto GenInfo che non è nullo".

Quindi, è impostare la proprietà Modello con un oggetto nullo:

this.view.Model = null; 

Naturalmente l'asserzione fallisce.

Nella tua seconda affermazione VerifySet

mockView.VerifySet(v => v.Model, Times.AtLeastOnce()); 

si stanno dicendo "affermare che la proprietà del modello è stata impostata, con qualsiasi cosa, almeno una volta". Dal momento che l'hai impostato (anche se con un valore nullo), ovviamente l'asserzione passa.

Non penso ci sia alcun errore nel comportamento di Moq qui.

Problemi correlati