My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using MockingFrameworksCompare.BrainSample;
using Moq;
using NUnit.Framework;

namespace MoqTests
{
[TestFixture]
public class BrainTests
{
/// <summary>
/// Verify that if hand throws an exception having touched a hot iron, <see cref="IMouth.Yell"/> gets called.
/// </summary>
/// <remarks>
/// Moq can mock both interfaces and classes - however, only virtual methods
/// of a class can be mocked (try changing IHand/IMouth to Hand/Mouth).
/// </remarks>
[Test]
public void TouchHotIron_Yell()
{
var hand = new Mock<IHand>();
var mouth = new Mock<IMouth>();
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());
}

/// <summary>
/// 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 <code>It.Is{Iron}(i => i.IsHot)</code>.
/// </summary>
//[Matcher]
//private static Iron HotIron() { return null; }
//public static bool HotIron(Iron iron) { return iron.IsHot; }
public Match<Iron> HotIron()
{
return new Match<Iron>(x => x.IsHot);
}
}
}
Show details Hide details

Change log

r37 by andreister on Feb 22, 2009   Diff
Catch up with the new approach for custom
matchers.
Go to: 
Project members, sign in to write a code review

Older revisions

r22 by andreister on Feb 18, 2009   Diff
Add CS tests and stubs for VB.
All revisions of this file

File info

Size: 1590 bytes, 43 lines
Hosted by Google Code