473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

raising/invoking events

I am experimenting with a class of widgets, and trying to decide on a
general strategy for testing them. Say my button widget has a property in
it's interface of:
public event EventHandler Click;

If I implement this as a Win Forms button, I know I can use the implemented
buttons PreformClick() method to test it, but how can I do this more
generically, just using the .Net event? For example, in the sample test code
below, I think I want to do something like
widgetHello.Click.Invoke(this, EventArgs.Empty);
which doesn't even compile much less work.

Thanks for the help - BH
[Test]
public void ClickEvent_CanBeDelegated_ToAnArbitraryMethod() {
// arrange
Assert.That(HelloMessage, Is.EqualTo(null));
var btnHello = new Button {Text = "Click Me"};
var widgetHello = new WinButton(btnHello);
widgetHello.Click += delegate { SayHello(); };
// act
btnHello.PerformClick();
// assert
Assert.That(HelloMessage, Is.EqualTo("Hello World"));
}
Oct 22 '08 #1
1 1354
There isn't a safe way of raising an arbitrary event. In this case,
one option might be to add an "internal" method that raises the event
- for example (extending the typical protected virtual OnFoo method):

public event EventHandler Foo;
protected internal virtual void OnFoo() {
if(Foo!=null) Foo(this,EventArgs.Empty);
}

then use the [InternalsVisibleTo] attribute to allow your test project
access to the internal method. Your test project can now call OnFoo()
to raise the event.

This only works if you own the type, of course.

Marc
Oct 22 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: serge calderara | last post by:
Dear all, I have a class wich is raising events as normally it should do. having a form in the same assembly wich is catching those events works fne. Raise events gets catch normaly within the...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
2
by: Catherine Jones | last post by:
Dear All Please help me in the following issue: how to raise an event from an unmanaged class to a managed class. Both classes are in C++ and there should not be any static methods involved in...
4
by: Joanna Carter \(TeamB\) | last post by:
I am trying to invoke an event using reflection. ////////////////////// public class Test { public event EventHandler NameChanged; public void CallEvent() {
2
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on...
9
by: CuriousGeorge | last post by:
Can someone explain why this code DOES NOT raise a null reference exception? //////////////////////////// Program.cs ///////////////////////////////////////////// using System; using...
23
by: Thomas Due | last post by:
Hi, I have a class which monitors a TCP socket. This will on occasion raise an event which can be handled by a GUI. Now, I am aware of the if(InvokeRequire) { EventHandler d = new...
3
by: =?Utf-8?B?c21rcmFtZXIwNzJmZGFzZmFzZGY=?= | last post by:
This questions is in reference to the article on Using Events in the C# Programmers Reference (http://msdn2.microsoft.com/en-us/library/ms173168.aspx). Under the Rasing Events section it says in...
26
by: buu | last post by:
So, let's say that a user enters an event name in text box, and would like to raise it.. is it possible to call events by their 'name'? Is it possible to enumerate events inside app. and to check...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.