Cannot Raise Events

9 posts, 0 answers
  1. Steve
    Steve avatar
    13 posts
    Member since:
    Feb 2013

    Posted 25 Mar 2013 Link to this post


    I am using the Professional version of JustMock and cannot seem to raise events.  Version 2013.1.220.3

    When I call Mock.Raise() I receive 
    • NullReferenceException was unhandled by user code.  Object reference not set to an instance of an object 



    using System;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using MockEvent;
    using Telerik.JustMock;
     
    namespace TestEvent
        [TestClass]
        public class JustMockTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                var mc = new MyClass();
                var args = new EventArgs();
     
                Mock.Raise(() => mc.TestEvent += null, args);
            }
        }
    }



    using System;
    using System.Linq;
     
    namespace MockEvent
    {
        public class MyClass
        {
            public event TestMethod TestEvent;
            public delegate void TestMethod(object sender, EventArgs args);       
     
            public MyClass()
            {
                TestEvent += MyClass_TestEvent;
            }
     
            void MyClass_TestEvent(object sender, EventArgs args)
            {
                Console.WriteLine("Running Event");
            }
        }
    }
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 26 Mar 2013 Link to this post

    Hello Steven,

    As replied in your previous thread with ID: 674260 (http://www.telerik.com/community/forums/justmock/general-discussions/mocking-sendmail-workflow-activity.aspx), you would need to instantiate a certain mock object to hold your arrange. I wrote the following example, that behaves as expected:
    [TestMethod]
    public void TestMethod1()
    {
        var mc = Mock.Create<MyClass>();
        var args = new EventArgs();
         
        Mock.Raise(() => mc.TestEvent += null, args);
    }

    I hope this helps. Please, notify us if you need further assistance.

    Greetings,
    Kaloyan
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
  4. Steve
    Steve avatar
    13 posts
    Member since:
    Feb 2013

    Posted 26 Mar 2013 Link to this post

    Kaloyan,  thanks for the response, but I am having problems running the following code.  It is extended a bit from the example posted earlier.  Mainly I exposed a function RunEvent() that will call 'TestEvent'.  If I create an instance of MyClass using Mock.Create, it will not fire the event when I call it directly (using RunEvent) or trying to Mock.Raise() the event.

    Using build 2013.1.315.0


    using System;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using MockEvent;
    using Telerik.JustMock;
     
    namespace TestEvent
        [TestClass]
        public class JustMockTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                var mc = Mock.Create<MyClass>(Behavior.CallOriginal);  // if i create a mock, even calling RunEvent fails to trigger the delegates attached to the event.
                var args = new EventArgs();
     
                mc.RunEvent(args);   // does not call event when instanced created by using Mock.Created
                // also if the above line is commented out, the line below will throw a NullReferenceException
                Mock.Raise(() => mc.TestEvent += null, args); 
            }
        }
    }




    using System;
    using System.Diagnostics;
    using System.Linq;
     
    namespace MockEvent
    {
        public delegate void TestHandler(object sender, EventArgs args);       
     
        public class MyClass
        {
            public event TestHandler TestEvent;
             
            public MyClass()
            {
                TestEvent += MyClass_TestEvent;
            }
     
            public void MyClass_TestEvent(object sender, EventArgs args)
            {
                // not being hit.
                Debug.WriteLine("Running Event");
            }
     
            public void RunEvent(EventArgs args)
            {
                if (TestEvent != null)
                    TestEvent(this, args);
            }
        }
    }
  5. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 27 Mar 2013 Link to this post

    Hello again Steven,

    Thank you for the detailed example.

    Investigating it, I noticed that "mc.RunEvent(args);" manages to raise the event every time. However, "Mock.Raise(() => mc.TestEvent += null, args);" fails. It appears that our current implementation of the Raise() method does not allow non-virtual events to be raised as expected.

    I have added this bug in our backlog and it should be fixed for future JustMock releases.  Please let me know if you want to be notified when a solution could be provided.

    As a workaround to the issue, at this point, I could only suggest making your event virtual. This should allow the using of our Mock.Raise() method in your tests.

    I have also attached couple of screenshots, to show that raising the event from the "RunEvent()" method works either way and the "Mock.Raise()" works only for virtual events.

    I hope this helps.

    P.S. For reporting such bug we have granted you some Telerik points.

    Kind regards,
    Kaloyan
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  6. Steve
    Steve avatar
    13 posts
    Member since:
    Feb 2013

    Posted 27 Mar 2013 Link to this post

    Yes, I would like to be notified.

    Unfortunately, this goes back my SendMail example, and I cannot make the SmtpClient.SendComplete event Virtual as I do not have access to those sources.

    Thanks for the explanation.
  7. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 28 Mar 2013 Link to this post

    Hello Steven,

    I understand that the issue is more or less a critical one and we apologize for the inconveniences caused by it.

    However, our development team is currently investigating it and a fix should be available soon. I will be happy to notify you once again when we are able to provide you with a solution.

    Thank you for the understanding.

    All the best,
    Kaloyan
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  8. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 05 Jul 2013 Link to this post

    Hi Steve,

    Just to notify you - this bug has been fixed ever since the 2013 Q2 release.

    Regards,
    Stefan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  9. Zeeshan
    Zeeshan avatar
    19 posts
    Member since:
    Jun 2013

    Posted 10 Aug 2013 Link to this post

    These all coding is very helpful for me
  10. Zeeshan
    Zeeshan avatar
    19 posts
    Member since:
    Jun 2013

    Posted 13 Aug 2013 Link to this post

    These all coding is very helpful for me
Back to Top
DevCraft R3 2016 release webinar banner