Counting the number of times an event is added to

4 posts, 0 answers
  1. Graham Allwood
    Graham Allwood avatar
    3 posts
    Member since:
    May 2010

    Posted 09 Nov 2012 Link to this post

    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
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 14 Nov 2012 Link to this post

    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.

  3. DevCraft R3 2016 release webinar banner
  4. Graham Allwood
    Graham Allwood avatar
    3 posts
    Member since:
    May 2010

    Posted 15 Nov 2012 Link to this post

    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.
  5. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 15 Nov 2012 Link to this post

    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.

Back to Top