using MockingFrameworksCompare.BrainSample; using Moq; using NUnit.Framework; namespace MoqTests { [TestFixture] public class BrainTests { /// /// Verify that if hand throws an exception having touched a hot iron, gets called. /// /// /// Moq can mock both interfaces and classes - however, only virtual methods /// of a class can be mocked (try changing IHand/IMouth to Hand/Mouth). /// [Test] public void TouchHotIron_Yell() { var hand = new Mock(); var mouth = new Mock(); hand.Setup(x => x.TouchIron(HotIron)).Throws(new BurnException()); var brain = new Brain(hand.Object, mouth.Object); brain.TouchIron(new Iron { IsHot = true }); mouth.Verify(x => x.Yell()); } /// /// Parameter expectations tend to be quite verbose in Moq, so we provide a custom matcher. /// This needs a matcher method and a bool sibling method for evaluating the expectations. /// Calling this matcher is technically equivalent to It.Is{Iron}(i => i.IsHot). /// public Iron HotIron { get { return Match.Create(x => x.IsHot); } } } }