While using NMock2, in many cases, you may not be able to easily guess exact parameter values, but you may have an idea about a portion of it. RegExMatcher class fills this gap.
NMock2 provides an easy way to create Mock objects to do your unit testing.
While setting up your expectations with NMock2, you may sometimes ignore the method parameters if you would like to have your function return always the same values.
Expect.Once.On(myMockObject).Method("MyMethod").Will(Return.Value("Always"));
However, if you would like to return different values based on the given parameters, then NMock2 requires you to type the exact parameter values that you would expect.
In many cases, you may not be able to easily guess exact parameter values, but you may have an idea about a portion of the parameter's value. NMock2 does not provide any functionality for that purpose. In order to fill that gap, you can use this small RegExMatcher class.
RegEx matcher allows you to create Regular Expression based Matchers for NMock2. Using RegExMatcher, you can easily create NMock2 expectations based on your Regular Expressions.
Here is a quick example:
Expect.Once.On(myMockObject).Method("MyMethod")
.With(new RegExMatcher("Case$"))
.Will(Return.Value("My Result"));
This NMock2 expectation will return "My Result" when MyMethod is called with a parameter that is ending with "Case". The portion, "Case$", is the Regular Expression part, you can use highly extended Regular Expressions based on your requirements.
Here is the code for RegExMatcher:
/**
* (c) Copyright 2007 A. Onur Cinar
* http://www2.zdo.com
*/
using System;
using System.IO;
using System.Text.RegularExpressions;
using NMock2;
namespace NMock2RegExMatcher
{
public class RegExMatcher : Matcher
{
#region Fields
Regex regex;
#endregion
#region Constructor
public RegExMatcher (string pattern)
: this(new Regex(pattern))
{
}
public RegExMatcher (Regex regex)
{
this.regex = regex;
}
#endregion
#region Methods
public override void DescribeTo (TextWriter writer)
{
writer.Write(regex.ToString());
}
public override bool Matches (object o)
{
return regex.Match(o.ToString()).Success;
}
#endregion
}
}
And here is an example for you to try:
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
using NMock2;
namespace NMock2RegExMatcher
{
public interface ITest
{
string Test (string text);
}
class Program
{
static void Main (string[] args)
{
const string RESULT1 =
"Ending with Case";
const string RESULT2 =
"Starting with Case and ending with Property";
// Setup the test
Mockery mockery = new Mockery();
ITest test = mockery.NewMock<ITest>();
Expect.Once.On(test).Method("Test")
.With(new RegExMatcher("Case$"))
.Will(Return.Value(RESULT1));
Regex regex = new Regex(
"^Case.*Property$",
RegexOptions.IgnoreCase);
Expect.Once.On(test).Method("Test")
.With(new RegExMatcher(regex))
.Will(Return.Value(RESULT2));
// Test our functions
string result = test.Test("//root/path/Case");
Trace.Assert(RESULT1.Equals(result));
result = test.Test("Case/City/State/Property");
Trace.Assert(RESULT2.Equals(result));
}
}
}
If you found this post helpful, please "Kick" it so others can find it too: