Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8729669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:52:26+00:00 2026-06-13T08:52:26+00:00

Having defined an interface public interface IHandlerViewModel { EventHandler ClearInputText { get; } }

  • 0

Having defined an interface

 public interface IHandlerViewModel {
         EventHandler ClearInputText { get; } 
}

I would like to test if ClearInputText is invoked by some method.
To do so I do something like this

SomeType obj=new SomeType();
bool clearCalled = false;
var mockHandlerViewModel=new Mock<IHandlerViewModel>();
mockHandlerViewModel.Setup(x => x.ClearInputText).Returns(delegate { clearCalled = true; });

obj.Call(mockHandlerViewModel.Object);//void Call(IHandlerViewModel);
Assert.IsTrue(clearCalled);

which fails. Simply the delegate is not called.
Please help me with this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:52:28+00:00Added an answer on June 13, 2026 at 8:52 am

    The example you give isn’t clear. You’re essentially testing your own mock.

    In a scenario where the mocked proxy is passed as a dependency to an object under test, you do no set up the event handler, you Raise it.

    var mockHandlerViewModel = new Mock<IHandlerViewModel>();
    var objectUnderTest = new ClassUnderTestThatTakesViewModel(mockHandlerViewModel.Object);
    // Do other setup... objectUnderTest should have registered an eventhandler with the mock instance. Get to a point where the mock should raise it's event..
    
    mockHandlerViewModel.Raise(x => x.ClearInputText += null, new EventArgs());
    // Next, Assert objectUnderTest to verify it did what it needed to do when handling the event.
    

    Mocks either substitute the event source by using .Raise(), or they substitute an object that will consume another class under test’s event (to assert the event was raised), in which case you use .Callback() to record “handling” the event in a local flag variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having defined this interface: public interface IInputBoxService<out T> { bool ShowDialog(); T Result {
I came across an interface recently that only defined a setter like so: public
I'm having a ListBox with some TextBlocks in its ItemTemplate. This TextBlocks are defined
I'm having something of an issue with trying to load externally defined classes in
I have an assembly A that defines an interface with some overloads: public interface
I'm having a little problem with jQuery: I'm having .submit() defined on a form,
I'm having a problem accessing an object defined in the root package. If I
I am having a problem accessing a static const variable defined in my class
I am having issues populating a List of User defined object attributes in Struts2.
I am having trouble getting a variable from one defined function to another. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.