question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Registering a handler on a fake Wrapping an event does not work

See original GitHub issue

Hello,

I want to be able to tell whether a method has been called on an object, but still use the actual implementation of this object.

To do this, I’m creating a fake for the interface, wrapping an instance of its implementation.

It works for methods, but not for events: trying to add an event handler on the faked event results in the concrete event never receiving the call to register the event handler.

Demo (the test fails):

[TestClass]
public class UnitTest1
{
	[TestMethod]
	public void TestMethod1()
	{
		var success = false;
		var instance = new MyClass();

		var fake = A.Fake<IInterface>(x => x.Wrapping(instance));

		fake.Event += (s, e) => success = true;

		fake.RaiseEvent();

		if (!success)
			Assert.Fail();
	}

	public interface IInterface
	{
		void RaiseEvent();
		event EventHandler Event;
	}

	public class MyClass : IInterface
	{
		public event EventHandler Event;
		public void RaiseEvent() => Event?.Invoke(this, EventArgs.Empty);
	}
}

I did not find any documentation about this behavior. Is this expected? Is there any way around that?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
blairconradcommented, Mar 15, 2021

This change has also been released as part of FakeItEasy 7.0.0.

1reaction
afakebotcommented, Dec 14, 2020

This change has been released as part of FakeItEasy 7.0.0-beta.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lighting aura:event not firing, or handler not executing
The problem: either the event seems to not be fired, or the handlers seem to not be activated. I put console. debug's, alerts...
Read more >
Workaround for generic event handler in Windows Forms
Best and very easy workaround is to subscribe the event in constructor programmatically than finding way to make the designer to work. Just...
Read more >
Bubbling and capturing
But any handler may decide that the event has been fully processed and stop the bubbling. The method for it is event.stopPropagation() ....
Read more >
JavaScript Event Handlers – How to Handle Events in JS
It gets handled by registering a function, called an event ... If useCapture is set to false , the event handler is in...
Read more >
Marking routed events as handled, and class handling
The KeyDown routed event is marked as handled. The instance handler attached to componentWrapper is triggered by the KeyDown routed event. The  ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found