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

Cannot use Mock<T>.Raise() on WPF Routed Events #61

Closed
perlun opened this issue Nov 26, 2013 · 1 comment
Closed

Cannot use Mock<T>.Raise() on WPF Routed Events #61

perlun opened this issue Nov 26, 2013 · 1 comment

Comments

@perlun
Copy link

perlun commented Nov 26, 2013

Hi,

Trying to unit test some WPF-related code and in that situation, I'm trying to get the code below to work:

var listView = new Mock<ListView>();
listView.Raise(lv => lv.Unloaded += null, EventArgs.Empty);

However, this gives me an ArgumentNullException with the following message:

Value cannot be null.Parameter name: handler 

Skipping the EventArgs.Empty makes no difference.

If I change my example to something like this:

void Main()
{
    var listView = new Mock<ListView>();
    listView.Raise(lv => lv.Unloaded += Foo);   
}

void Foo(object sender, RoutedEventArgs e)
{
}

...I get a different exception (ArgumentException).

Expression is not an event attach or detach, or the event is declared in a
class but not marked virtual

So, is the conclusion basically that we cannot mock these events, since they aren't virtual? That's what the exception message is telling me, but I just want to check... Are there any other realistic test scenarios for 3rd party classes which uses events?

@kzu
Copy link
Member

kzu commented Nov 26, 2013

Events are no different than everything else. They have to be virtual to be mockable.

@kzu kzu closed this as completed Nov 26, 2013
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

No branches or pull requests

2 participants