This is a migrated thread and some comments may be shown as answers.

Counting the number of times an event is added to

3 Answers 30 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Graham Allwood
Top achievements
Rank 1
Graham Allwood asked on 09 Nov 2012, 12:25 PM
Hi,

is there a way for me to make sure my event in my class being tested is added to? For example, if the class I am tests has a method like:

public void Work(int count)
{
    if(count == 1)
    {
        MyEvent += new MyHandler(handleEvent);
    }
}

I want to test when 1 is passed in to this method the handler (any handler) is attched to Myevent. Is this possible?
Thanks

Graham

3 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 14 Nov 2012, 12:54 PM
Hi Graham,

 Thank you for contacting Telerik support.

 In order to help you with your issue, I would recommend you to check the following example:
public class Foo
{
    public event EventHandler<NumberEventArgs> E;
 
    public void TrigerE(int i)
    {
        var handler = E;
        if (handler != null)
        {
            handler(this, new NumberEventArgs(i));
        }
    }
}
 
public class NumberEventArgs : EventArgs
{
    public int TheNumber { get; private set; }
 
    public NumberEventArgs(int num)
    {
        this.TheNumber = num;
    }
}
 Assume that, the first class("Foo") is holding the event and the second one("NumberEventArgs") is providing the args.

 If we need to test if the event handler is attached to the event, for a certain argument, using JustMock we can do it like this:
[TestMethod]
public void TestMethod2()
{
    var mock = Mock.Create<Foo>(Behavior.CallOriginal);
 
    var event_E_is_called_with_one = false;
    mock.E += (s, e) => { event_E_is_called_with_one = e.TheNumber == 1; };
 
    mock.TrigerE(2);
 
    Assert.IsFalse(event_E_is_called_with_one);
 
    mock.TrigerE(1);
 
    Assert.IsTrue(event_E_is_called_with_one);
}
 I hope this solves your issue. However, if there is anything else we could help you with, please do not hesitate to ask.

Greetings,
Kaloyan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Graham Allwood
Top achievements
Rank 1
answered on 15 Nov 2012, 10:10 AM
Thanks for the reply.

I understand what you are doing here. To summarise; the only way to tell if an event has had a handler attached to it is to raise the event and make sure the handler is called. In this situation, the handler would be a mocked object thus allow the unit test to validate it was called.
0
Kaloyan
Telerik team
answered on 15 Nov 2012, 01:29 PM
Hi Graham,

 This is exactly the point you need to follow in order to handle such situations.

 Please let us know, if you need more help, about our product. 

Kind regards,
Kaloyan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
General Discussions
Asked by
Graham Allwood
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Graham Allwood
Top achievements
Rank 1
Share this question
or