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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:18:23+00:00 2026-05-17T23:18:23+00:00

Suppose you had 2 classes, Listener and Talker. Talker has an event Talking, and

  • 0

Suppose you had 2 classes, Listener and Talker. Talker has an event Talking, and when this event is fired, Listener should execute a void method HeardTalk, along these lines:

public class Talker
{
   public event EventHandler Talking;

   public void Talk()
   {
      if (Talking != null)
      {
         Talking(this, null);
      }
   }
}

public class Listener
{
   public void StartListening(Talker talker)
   {
      talker.Talking += HeardTalk;
   }

   public void HeardTalk(object sender, EventArgs e)
   {
      // do something private here
   }
}

How would you go about unit testing that once StartListening has been called, HeardTalk gets called, if there was no public state reflecting that the method was called? I could just add such a state for the purpose of validation, but it seems clumsy. Ideally, I would like to assert the call was made, in a manner similar to what Mocking frameworks do, but I can’t mock the class under test.

Is there an elegant way to assert that a method was called on the SUT, without modifying it just for the purpose of testability?

  • 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-17T23:18:23+00:00Added an answer on May 17, 2026 at 11:18 pm

    You can do such things easily with the commercial Typemock Isolator, which allows you to selectively ‘mock’ individual methods on ‘real’ objects.
    The test would look like this:

    [Test, Isolated]
    public void HeardTalk_GetsCalled()
    {
        // --- Arrange ---
        var talker = new Talker();
        var listener = new Listener();
        bool heardTalkWasCalled = false;
        Isolate.WhenCalled(() => listener.HeardTalk(null, null))                     // Selectively 'mock' the call to 'listener.HeardTalk()'
                                         .DoInstead(x => heardTalkWasCalled = true); // on the live object (arguments are ignored by default)
    
        // --- Act ---
        listener.StartListening(talker);
        talker.Talk();
    
        // --- Assert ---
        Assert.IsTrue(heardTalkWasCalled);
    }
    

    Admittedly, Typemock Isolator comes with some license costs. But if you’re serious about testing and need to test a lot of stuff like the above, it’s worth every penny because of its strength and flexibility.

    Notes:

    • You can do the same with the free MS Moles framework (for Visual Studio 2010), which is a suitable alternative to Typemock, if you have to do such tests only occasionally. It generally requires somewhat more work/code, adds more complexity to the tests, and is more performance-demanding especially during compilation, but is sufficient for smaller test suites.
    • There is also a commercial alternative from Telerik called JustMock. Because it is quite new, I can’t say nothing about it except that it exists…

    HTH!
    Thomas

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

Sidebar

Related Questions

Consider an interface that has only one method: Object getValue(String data); Suppose this describes
Suppose you had this: def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName
Suppose you had code like this: _READERS = None _WRITERS = None def Init(num_readers,
Suppose I had this Directed Acyclic Graph (DAG) , where there is a directed
suppose I had something like this: L1=['cat', 'dog', 'fish', 'rabbit', 'horse', 'bird', 'frog', 'mouse'...]
Suppose I had a string for example: >>> stri = "日本" >>> res =
Suppose I had a WCF service that I have coded up, like Clemens Vasters's
For example, suppose I had written my own class which allowed me to record
Suppose I only had the regular J2SE http libraries but wanted to write a
Suppose I'm making a JDBC call and I had fetched data from a database

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.