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 862353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:07:39+00:00 2026-05-15T09:07:39+00:00

I need to create some unit tests that confirm that instance of a class

  • 0

I need to create some unit tests that confirm that instance of a class responds appropriately to an event raised by another object. Passing in a mock of the ‘watched’ object is easy enough, but as far as I can tell, mocks in nunit don’t provide a means of having the mock raise an event.

I’m considering using reflection to directly invoke the event handler on the object I’m testing, passing a reference to the mock as the ‘sender’, but this doesn’t effectively test that my object correctly hooked the events in the first place (although now I think about it, this should probably be tested separately anyway).

It does seem like a bit of a hack, but I can’t see another way of doing it; is there a better way? Also, is it possible for a mock to check that an event is hooked?

  • 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-05-15T09:07:40+00:00Added an answer on May 15, 2026 at 9:07 am

    The RhinoMock code is great for verifying that subscription happens or for verifying that events are raised, but not so great for raising events itself.

    I have a similar issue. I got around it by writing my own little stub class that implements the event aggregator interface and allows me to raise an event from the test. Sometimes writing your own test code can be easier than messing around with mocking frameworks, which weren’t really designed to do this.

    The behaviour of your class is only valuable if it hooks to the events and responds to them, so I don’t think there’s much point in testing these separately.

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

Sidebar

Related Questions

I need to unit tess a method that receives a MethodInfo object of some
I need to create some xaml that shows text in columns like newspaper, something
Into a class constructor, I need to create some objects on the fly and
I need to create a DIV that has some content (some of which is
I'm writing some unit tests and I need to be able to Assert whether
I am writing some unit tests in which I need a fake xml file.
I'm writing some unit tests for my View Model class. The constructor of this
I'm trying to find a way to write some unit-tests that can be used
I'm doing some unit tests where essentially I need the input stream to block
I have some Web API methods that I want to write unit tests 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.