| /trunk/Tests/CS/MoqTests/BrainTests.cs r22 | /trunk/Tests/CS/MoqTests/BrainTests.cs r37 | ||
| 1 | using MockingFrameworksCompare.BrainSample; | 1 | using MockingFrameworksCompare.BrainSample; |
|---|---|---|---|
| 2 | using Moq; | 2 | using Moq; |
| 3 | using NUnit.Framework; | 3 | using NUnit.Framework; |
| 4 | 4 | ||
| 5 | namespace MoqTests | 5 | namespace MoqTests |
| 6 | { | 6 | { |
| 7 | [TestFixture] | 7 | [TestFixture] |
| 8 | public class BrainTests | 8 | public class BrainTests |
| 9 | { | 9 | { |
| 10 | /// <summary> | 10 | /// <summary> |
| 11 | /// Verify that if hand throws an exception having touched a hot iron, <see cref="IMouth.Yell"/> gets called. | 11 | /// Verify that if hand throws an exception having touched a hot iron, <see cref="IMouth.Yell"/> gets called. |
| 12 | /// </summary> | 12 | /// </summary> |
| 13 | /// <remarks> | 13 | /// <remarks> |
| 14 | /// Moq can mock both interfaces and classes - however, only virtual methods | 14 | /// Moq can mock both interfaces and classes - however, only virtual methods |
| 15 | /// of a class can be mocked (try changing IHand/IMouth to Hand/Mouth). | 15 | /// of a class can be mocked (try changing IHand/IMouth to Hand/Mouth). |
| 16 | /// </remarks> | 16 | /// </remarks> |
| 17 | [Test] | 17 | [Test] |
| 18 | public void TouchHotIron_Yell() | 18 | public void TouchHotIron_Yell() |
| 19 | { | 19 | { |
| 20 | var hand = new Mock<IHand>(); | 20 | var hand = new Mock<IHand>(); |
| 21 | var mouth = new Mock<IMouth>(); | 21 | var mouth = new Mock<IMouth>(); |
| 22 | hand.Setup(x => x.TouchIron(HotIron())).Throws(new BurnException()); | 22 | hand.Setup(x => x.TouchIron(HotIron())).Throws(new BurnException()); |
| 23 | 23 | ||
| 24 | var brain = new Brain(hand.Object, mouth.Object); | 24 | var brain = new Brain(hand.Object, mouth.Object); |
| 25 | brain.TouchIron(new Iron { IsHot = true }); | 25 | brain.TouchIron(new Iron { IsHot = true }); |
| 26 | 26 | ||
| 27 | mouth.Verify(x => x.Yell()); | 27 | mouth.Verify(x => x.Yell()); |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | /// <summary> | 30 | /// <summary> |
| 31 | /// Parameter expectations tend to be quite verbose in Moq, so we provide a custom matcher. | 31 | /// Parameter expectations tend to be quite verbose in Moq, so we provide a custom matcher. |
| 32 | /// This needs a matcher method and a bool sibling method for evaluating the expectations. | 32 | /// This needs a matcher method and a bool sibling method for evaluating the expectations. |
| 33 | /// Calling this matcher is technically equivalent to <code>It.Is{Iron}(i => i.IsHot)</code>. | 33 | /// Calling this matcher is technically equivalent to <code>It.Is{Iron}(i => i.IsHot)</code>. |
| 34 | /// </summary> | 34 | /// </summary> |
| 35 | [Matcher] | 35 | //[Matcher] |
| 36 | private static Iron HotIron() { return null; } | 36 | //private static Iron HotIron() { return null; } |
| 37 | public static bool HotIron(Iron iron) { return iron.IsHot; } | 37 | //public static bool HotIron(Iron iron) { return iron.IsHot; } |
| 38 | public Match<Iron> HotIron() | ||
| 39 | { | ||
| 40 | return new Match<Iron>(x => x.IsHot); | ||
| 41 | } | ||
| 38 | } | 42 | } |
| 39 | } | 43 | } |