-
-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #891 from stakx/eventhandlercollection
Make mock event subscription equally strict as regular event subscription
- Loading branch information
Showing
4 changed files
with
86 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD. | ||
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. | ||
|
||
using System; | ||
|
||
using Xunit; | ||
|
||
namespace Moq.Tests | ||
{ | ||
public class EventHandlerTypesMustMatchFixture | ||
{ | ||
[Fact] | ||
public void CLI_requires_event_handlers_to_have_the_exact_same_type() | ||
{ | ||
var mouse = new Mouse(); | ||
var result = 2; | ||
|
||
mouse.LeftButtonClicked += new Action<LeftButton>(_ => result += 3); | ||
mouse.LeftButtonClicked += new Action<LeftButton>(_ => result *= 4); | ||
mouse.RaiseLeftButtonClicked(new LeftButton()); | ||
|
||
Assert.Equal(20, result); | ||
} | ||
|
||
[Fact] | ||
public void CLI_throws_if_event_handlers_do_not_have_the_exact_same_type() | ||
{ | ||
var mouse = new Mouse(); | ||
mouse.LeftButtonClicked += new Action<Button>(delegate { }); | ||
Assert.Throws<ArgumentException>(() => mouse.LeftButtonClicked += new Action<LeftButton>(delegate { })); | ||
} | ||
|
||
[Fact] | ||
public void Moq_requires_event_handlers_to_have_the_exact_same_type() | ||
{ | ||
var mouseMock = new Mock<Mouse>(); | ||
var mouse = mouseMock.Object; | ||
var result = 2; | ||
|
||
mouse.LeftButtonClicked += new Action<Button>(_ => result += 3); | ||
mouse.LeftButtonClicked += new Action<Button>(_ => result *= 4); | ||
mouseMock.Raise(m => m.LeftButtonClicked += null, new LeftButton()); | ||
|
||
Assert.Equal(20, result); | ||
} | ||
|
||
[Fact] | ||
public void Moq_throws_if_event_handlers_do_not_have_the_exact_same_type() | ||
{ | ||
var mouseMock = new Mock<Mouse>(); | ||
var mouse = mouseMock.Object; | ||
|
||
mouse.LeftButtonClicked += new Action<Button>(delegate { }); | ||
Assert.Throws<ArgumentException>(() => mouse.LeftButtonClicked += new Action<LeftButton>(delegate { })); | ||
} | ||
|
||
public class Mouse | ||
{ | ||
public virtual event Action<LeftButton> LeftButtonClicked; | ||
|
||
public void RaiseLeftButtonClicked(LeftButton button) | ||
{ | ||
this.LeftButtonClicked?.Invoke(button); | ||
} | ||
} | ||
|
||
public abstract class Button { } | ||
|
||
public class LeftButton : Button { } | ||
} | ||
} |