Moq error - parameter counter mismatch

Background

I have the following script:

  • A MailItemProxy class with a constructor with a single argument of type MailItem (MailItem is actually part of the Microsoft.Office.Interop.Outlook namespace)
  • An event named PropertyChange (from the Outlook MailItem class) is registered inside the MailItemProxy constructor :
public MailItemProxy(MailItem mailItem)
{
    this.mailItem = mailItem;
    this.mailItem.PropertyChange += this.MailItem_PropertyChange;
}

My MailItemProxy class implements INotifyPropertyChanged, so it has its own PropertyChanged event (note that this is a "PropertyChanged", not the Outlook MailItem "PropertyChange" native time).

The * MailItem_PropertyChange * event handler looks like this:

 private void MailItem_PropertyChange(string name)
 {
     if (this.PropertyChanged != null)
     {
         this.PropertyChanged(this, new PropertyChangedEventArgs(name));
     }
 }

Intention

- , MailItem PropertyChange, (MailItemProxy) .

Moq.

" " "", PropertyChange mailItemStub. PropertyChange , ItemEvents_10_PropertyChangeEventHandler ( ) Microsoft.Office.Interop.Outlook. Arrange mailItemProxy, Act - , , , -, , .

, ?

 [TestMethod]
 public void PropertyChanged_WhenMailItemPropertyChange_EventIsCalled()
 {
     // Arrange
     bool eventDispatched = false;
     var mailItemStub = new Mock<MailItem>();
     var mailItemProxy = new MailItemProxy(mailItemStub.Object);
     mailItemProxy.PropertyChanged += (sender, args) => { eventDispatched = true; };

     // Act
     mailItemStub.Raise(x => x.PropertyChange += (name) => { });

     // Assert
     Assert.IsTrue(eventDispatched);
 }

Test Name:  PropertyChanged_WhenMailItemPropertyChange_EventIsCalled
Test Outcome:   Failed
Test Duration:  0:00:00.2764026

Result Message: 
Test method UI.Office.UnitTests.MailItemProxyTest.PropertyChanged_WhenMailItemPropertyChange_EventIsCalled threw exception: 
System.Reflection.TargetParameterCountException: Parameter count mismatch.
Result StackTrace:  
at Moq.Mock`1.Raise(Action`1 eventExpression, Object[] args)
   at UI.Office.UnitTests.MailItemProxyTest.PropertyChanged_WhenMailItemPropertyChange_EventIsCalled()
+4
1

Mock.Raise - EventArgs, params object[] args. string name :

.

 mailItemStub.Raise(x => x.PropertyChange += (name) => { }, "FooBar");

+5

Source: https://habr.com/ru/post/1526371/


All Articles