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

Events not working with CallBase = true depending on order interfaces are inherited. C : IA, IB behaves different than C : IB, IA #228

Closed
seanhalliday opened this issue Dec 16, 2015 · 3 comments · Fixed by #288

Comments

@seanhalliday
Copy link

public interface IFoo
{
event EventHandler Foo;
}

public interface IBar
{
event EventHandler Bar;
}

// reverse the order of IFoo and IBar will change the results of the test
public class FooBar : IFoo, IBar
{
public event EventHandler Foo;
public event EventHandler Bar;
public void RaiseEvents()
{
if (Foo != null)
Foo(this, EventArgs.Empty);
if (Bar != null)
Bar(this, EventArgs.Empty);
}
}

[TestClass]
public class MoqTest
{
[TestMethod]
public void MultipleInterfacesWithEvents()
{
var mock = new Mock<FooBar>() { CallBase = true };
IFoo foo = mock.Object;
IBar bar = mock.Object;

           bool fooCalled = false;
           bool barCalled = false;
           foo.Foo += (s, e) => fooCalled = true;
           bar.Bar += (s, e) => barCalled = true;

           mock.Object.RaiseEvents();

           Assert.IsTrue(fooCalled);
           Assert.IsTrue(barCalled);
    }

}

@pjquirk
Copy link

pjquirk commented Dec 16, 2015

@kzu
Copy link
Contributor

kzu commented Dec 17, 2015

IIRC, if you manually inherit a class with just virtual events but no
method to raise the event, it's impossible to raise it "from the outside".
The best you can do is what Moq does, calling the invocation list manually.
There is no magic in Moq, if you can do it with custom code, we should be
able to do it with Moq.

On Wed, Dec 16, 2015, 3:55 PM Patrick Quirk notifications@github.com
wrote:

Possibly related question/analysis on SO
http://stackoverflow.com/a/34319714/1698557.


Reply to this email directly or view it on GitHub
#228 (comment).

@kzu
Copy link
Contributor

kzu commented Dec 17, 2015

Could you update the issue title and provide a better description then?

Thanks

On Thu, Dec 17, 2015, 12:36 AM seanhalliday notifications@github.com
wrote:

If you read the rest of my comments on the issue, you will see that moq
behaves correctly if we simply reorder the interfaces listed in the
inheritance,so it is definitely a bug in moq.
On Dec 16, 2015 6:08 PM, "Daniel Cazzulino" notifications@github.com
wrote:

IIRC, if you manually inherit a class with just virtual events but no
method to raise the event, it's impossible to raise it "from the
outside".
The best you can do is what Moq does, calling the invocation list
manually.
There is no magic in Moq, if you can do it with custom code, we should be
able to do it with Moq.

On Wed, Dec 16, 2015, 3:55 PM Patrick Quirk notifications@github.com
wrote:

Possibly related question/analysis on SO
http://stackoverflow.com/a/34319714/1698557.


Reply to this email directly or view it on GitHub
#228 (comment).


Reply to this email directly or view it on GitHub
#228 (comment).


Reply to this email directly or view it on GitHub
#228 (comment).

@seanhalliday seanhalliday changed the title Events not working with CallBase = true when object is cast to interface that defines the event Events not working with CallBase = true depending on order interfaces are inherited. C : IA, IB because different than C : IB, IA Dec 17, 2015
@seanhalliday seanhalliday changed the title Events not working with CallBase = true depending on order interfaces are inherited. C : IA, IB because different than C : IB, IA Events not working with CallBase = true depending on order interfaces are inherited. C : IA, IB behaves different than C : IB, IA Dec 17, 2015
bradreimer added a commit to bradreimer/moq4 that referenced this issue Sep 20, 2016
@kzu kzu closed this as completed in #288 Sep 20, 2016
kzu pushed a commit that referenced this issue Sep 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants