-
-
Notifications
You must be signed in to change notification settings - Fork 803
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
Mock<T>.Raise only raises events on root object #166
Comments
This issue also exists when the root mock and the child mock don't have events with the same name. WorkaroundRetrieve the child mock from the root mock and raise the event on the child mock:
This works and leads to the expected results. Issue AnalysisUpon a first look into the implementation, it seems that the |
@BrunoJuchli: Thank you for the analysis, this is exactly the reason why events on child mocks don't get raised. I had already prepared a fix for this when I discovered this test from 2009, which is now failing: [Fact]
public void DoesNotRaiseEventOnSubObject()
{
var mock = new Mock<IParent> { DefaultValue = DefaultValue.Mock };
bool raised = false;
mock.Object.Adder.Added += (sender, args) => raised = true;
Assert.Same(mock.Object.Adder, mock.Object.Adder);
mock.Raise(p => p.Adder.Added += null, EventArgs.Empty);
Assert.False(raised);
} According to this, not being able to raise events on child mocks is expected behavior. As mentioned by @hallipr, the quickstart guide (which was started in late 2013) says differently. Something needs to be fixed here:
I'd say we go ahead with (2). |
@stakx Also, I think there's some cases where supporting to raise events on child mocks would be helpful - i experienced it at least once. Therefore I'd, too, vote for number 2. |
@BrunoJuchli, you're right on all accounts, I didn't phrase the breakage scenario very well. Glad to hear you agree on (2). I've just now merged the required changes, so this will be resolved in Moq >4.7.63. |
This behavior is still present in 4.10.0. Is it going to be fixed? |
I've found an extrange behavior on firing events with Moq.
When mockThermostat.Raise(...) fires the event all is correct except the cases with event args as the only parameter, it always throws the previous mentioned exception: Am I missing any thing? |
@BrunoJuchli Here is the code for reproducing the behavior:
In case the OwnerBool is changed (the commented line), the event is fired as expected. But when the FooBoolean is changed the event on IEventOwner is not fired as expected (I think that it is being searched for in IFoo...). |
@yordans - I haven't digged very deeply yet, however it appears that the problem was fixed only for The following lines of code look relevant. Notice how the "target" (i.e. the object on which the event sits) simply get discarded: At this point I am not yet sure whether and how to fix this (if a fix is feasible at all). For now, I can only advise not to get too carried away with the possibilities Moq appears to offer here. Remember, under normal circumstances, you wouldn't be able either to just trigger an event from the outside. (I am not so sure that it was wise to add that ability to Moq, as it might steer people to less-than-ideal code patterns.) |
@yordans - I have closed this issue because your test case is actually caused by a different problem. You manually set up the So what you can do is this: -var fooMock = new Mock<IFoo>();
+var fooMock = new Mock<IFoo>() { DefaultValue = DefaultValue.Mock };
-fooMock.Setup(f => f.EventOwner).Returns(eventOwnerMock.Object);
fooMock.SetupSet(f => f.FooBoolean = It.IsAny<bool>()).Raises(f => f.EventOwner.OwnerEvent += null, EventArgs.Empty);
bool isEventRaised = false;
fooMock.Object.EventOwner.OwnerEvent += (s, e) => isEventRaised = true; The last line shown above, together with By the way, this limitation is likely going to be improved soonish. |
Resolved in 4.11.0-rc1. |
https://github.com/Moq/moq4/wiki/Quickstart#events
Example 2, "descendant events", doesn't function as expected
Events on the root object are raised when trying to raise events on the child.
This is noticed in 1/17/2013
http://stackoverflow.com/questions/14386540/mocked-inner-objects-events-will-not-fire
As tested in LINQPad:
Expected output:
Actual output:
All events named "Event" raise IFoo.Event.
Returns:
The text was updated successfully, but these errors were encountered: