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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:18:18+00:00 2026-05-30T06:18:18+00:00

Lets say I have a eventhandler like so: public SomethingHappended_Handler(object sender, EventArgs e) {

  • 0

Lets say I have a eventhandler like so:

public SomethingHappended_Handler(object sender, EventArgs e)
{
  var myobj = sender as MyClass();
  myobj.DoSomethingImportant();
}

How can I test that myObj is the type of MyClass? I know that the sender will always be of MyClass but I just thought to be safe I would need a:

if (myObj != null) //Close it

If the type is not MyClass then myObj will be null. As I know its best to test for the not null how can I create a unit test to prove that if I send a different type as the sender other than MyClass it will fall over calling DoSomethingImportant and therefore I need the null check?

  • 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-30T06:18:20+00:00Added an answer on May 30, 2026 at 6:18 am

    I can answer you in pseudo-code to point out what you could do.

    First off you can check if sender is of type MyClass.

    if(sender is MyClass)
    

    Then in your unittest you can send in two mock objects, one of type MyClass and one of another type “MyClassFake”. Make an interface both use and make them implement the Close() method.

    Make two unittests:

    1. Validate that MyClass.Close() is called.
    2. Validate that MyClassFake.Close() is never called.

    Close() is just an example, if you have other methods, you can test called/not called against them.

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

Sidebar

Related Questions

Lets say I have a class that is doing something like: public class Foo
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Lets say I have an array like this: string [] Filelist = ... I
Lets say I have a single object of type Car which I want to
Lets say we have something like: <div class=row> <div class=box> <a class=more href=#more/> </div>
Lets say i have the following set of interfaces.... public interface IStart { void
Lets say i have created a small game with XNA. Now i can change
Lets say I have a div like this: <div id=test class=sourcecode> Some String </div>
Lets say I have a controller which provides search functions for a student: public

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.