My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
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>
public Iron HotIron
{
get { return Match<Iron>.Create(x => x.IsHot); }
}
}
}

Change log

r42 by andreister on Mar 8, 2009   Diff
Upgrade Moq tests.
Go to: 
Project members, sign in to write a code review

Older revisions

r37 by andreister on Feb 22, 2009   Diff
Catch up with the new approach for
custom matchers.
r22 by andreister on Feb 18, 2009   Diff
Add CS tests and stubs for VB.
All revisions of this file

File info

Size: 1439 bytes, 40 lines
Powered by Google Project Hosting