Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raising internal events silently fails #1560

Closed
mriehm opened this issue Jan 12, 2019 · 3 comments
Closed

Raising internal events silently fails #1560

mriehm opened this issue Jan 12, 2019 · 3 comments
Assignees
Labels
Milestone

Comments

@mriehm
Copy link

mriehm commented Jan 12, 2019

I have followed the FakeItEasy documentation to be able to fake internal types, which works for most of my tests. However, when registering a callback to a faked internal event, the callback registration is silently failing, so when the event is later raised then the expected callback is mysteriously not invoked.

Here is a minimal repro:

internal class PublicClass
{
	internal virtual event EventHandler<EventArgs> InternalEvent;

	internal virtual void InternalCallback(object sender, EventArgs e) { }
}

[TestMethod]
public void InternalEventTest()
{
	PublicClass publicClass = A.Fake<PublicClass>();
	publicClass.InternalEvent += publicClass.InternalCallback;
	publicClass.InternalEvent += Raise.With(new EventArgs());

	A.CallTo(() => publicClass.InternalCallback(A<object>._, A<EventArgs>._)).MustHaveHappened();
}

As written, this test fails the MustHaveHappened() check. However, if the internal event is changed from internal to public, then the test passes (even while leaving the class itself as internal!).

I have determined the code responsible is in FakeManager.EventRule.cs. In that file, if the call to Type.GetEvents() is changed to specify BindingFlags.NonPublic, and if the calls to EventInfo.GetAddMethod/GetRemoveMethod are changed to pass true for the nonpublic parameter, then the above test passes.

@thomaslevesque
Copy link
Member

Hi @mriehm,

Thanks for reporting this and providing detailed repro steps. It looks like a bug. We'll investigate and come back to you.

@blairconrad
Copy link
Member

Thanks, @mriehm. Very nice job reporting and pointing us at the fix.

One thing I should note is that the link you pasted to the docs is actually to what I had thought was a private, non-visible docs build that I'd set up while experimenting with readthedocs. They're quite outdated (but the article you pointed at is close enough to the current behaviour). The official docs are at https://fakeiteasy.readthedocs.io/. I'll see about getting rid of my outdated docs!

@blairconrad blairconrad mentioned this issue Jan 12, 2019
12 tasks
@blairconrad
Copy link
Member

This change has been released as part of FakeItEasy 5.0.1.

Thanks for reporting and for the suggested solution, @mriehm. Look for your name in the release notes! 🏆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants