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

Cannot Raise Events

8 Answers 107 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 25 Mar 2013, 03:53 PM

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");
        }
    }
}

8 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 26 Mar 2013, 12:49 PM
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.
0
Steve
Top achievements
Rank 1
answered on 26 Mar 2013, 05:51 PM
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);
        }
    }
}
0
Kaloyan
Telerik team
answered on 27 Mar 2013, 04:51 PM
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.
0
Steve
Top achievements
Rank 1
answered on 27 Mar 2013, 07:44 PM
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.
0
Kaloyan
Telerik team
answered on 28 Mar 2013, 09:20 AM
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.
0
Stefan
Telerik team
answered on 05 Jul 2013, 08:20 AM
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.
0
Zeeshan
Top achievements
Rank 1
answered on 10 Aug 2013, 07:38 PM
These all coding is very helpful for me
0
Zeeshan
Top achievements
Rank 1
answered on 13 Aug 2013, 11:18 AM
These all coding is very helpful for me
Tags
General Discussions
Asked by
Steve
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Steve
Top achievements
Rank 1
Stefan
Telerik team
Zeeshan
Top achievements
Rank 1
Share this question
or