Time has come for another Isolator release - this time we've put a lot of effort into providing features and bug fixes requested by many of you - hope you like it! The new version is available on our download page.
Major features:
Exact Argument Matching
You now have the power to provide a fake behavior that will only apply when the arguments passed to the method being faked match those passed to WhenCalled(). This is how it looks like in our c# API (WillThrow is used to illustrate the feature. You can of course use any behavior):
var fake = Isolate.Fake.Instance<Logger>();
Isolate.WhenCalled(() => fake.OpenPath("c:\log.txt")).WithExactArguments(). WillThrow(new FileNotFoundException()); // this call will be ignored fake.OpenPath("c:\foo.txt"); // this call will throw an exception fake.OpenPath("c:\log.txt");And for you VBers:
Dim fake As Logger = FakeInstance(Of Logger)()
Using TheseCalls.WithExactArguments.WillThrow(new FileNotFoundException()) fake.OpenPath("c:\log.txt") End Using' This call is ignored fake.OpenPath("c:\foo.txt") ' This call throws an exception fake.OpenPath("c:\log.txt")Redefining Methods
Up until now calls could be faked to stock behaviors such as returning a value, throwing an exception, ignoring the call etc. However, for some of you this wasn't enough - you wanted to control custom method behavior and define what happens when a method is called yourself. Well, now you can, using this new API:
var fake = Isolate.Fake.Instance<ProductFactory>();
// define GetProductSequenceId() to return a custom ID var count = 0;
var countMyProduct = 0;
Isolate.WhenCalled(() => fake.GetProductSequenceId("")). DoInstead(context =>
{ // return a count of "MyProduct"s created so farif(context.Parameter[0] as string == "MyProduct")
{ return countMyProdcut++;}
return count++; });
// returns 0 fake.GetProductSequenceId("MyProduct"); // returns 1 fake.GetProdcutSequenceId("MyProduct"); // returns 0 fake.GetProductSequenceId("OtherProduct");The same functionality in VB.NET is accessed using TheseCalls.WillBeReplacedWith(). Because lambda expressions are not fully supported in VB.NET, we pass in a delegate address:
Using TheseCalls.WillBeReplacedWith(AddressOf CustomSequenceId) fake.GetProductSequenceId()
End Using' this is our custom behavior implementation Shared count As Integer = 0
Shared countMyProduct as Integer = 0
Public Function CustomSequenceId(ByVal context as MethodCallContext) As Integer
If CType(context.Parameter(0), String) = "MyProduct" Then
countMyProduct = countMyProduct + 1
Return countMyProductEnd If
count = count + 1
Return countEnd FunctionSpecific Constructor Invocation
Previously, when creating fake objects you had little control over the object's constructor behavior; either the default constructor (or the simplest constructor found) was called (when creating fakes with Members.CallOriginal), or no constructor was called. Now you can easily decide whether or not to call a constructor, and which parameters to pass to a constructor if called.
In c# it looks like this:
// do not call a c'tor for a CallOriginal fake objectvar fake1 = Isolate.Fake.Instance<Logger>(Members.CallOriginal, ConstructorWillBe.Ignored);
// call a specific c'tor for a RecursiveFakes fake object
var fake2 = Isolate.Fake.Instance<Logger>(Memebrs.ReturnRecursiveFakes, ConstructorWillBe.Called, "c:\log.txt");And in VB.NET:
' Create a CallOriginal fake object but do not call its c'tor Dim fake1 As Logger = FakeInstance(Of Logger)(Members.CallOriginal, ConstructorWillBe.Ignored)
' Create a RecursiveFakes fake object and call a specific c'tor Dim fake2 As Logger = FakeInstance(Of Logger)(Members.ReturnRecursiveFakes, ConstructorWillBe.Called, "c:\log.txt")Bug Fixes
- Several issues concerning faking generic objects have been addressed
- NCover 3 (starting version 3.0.18) linking integration is now supported
As you can see, there are some nice new tools in this version, and I can tell you we're pretty pleased with it. Go ahead and get it, and let us know what you think!






3 comments:
Isolate.WhenCalled(() => fake.GetProductSequence("")).
DoInstead(context =>
{
// return a count of "MyProduct"s created so far
if(context.Parameter[0] as string == "MyProduct")
{
return countMyProdcut++;
}
return count++;
});
// returns 0
fake.GetProductSequenceId("MyProduct");
The fake.GetProductSequence should be fake.GetProductSequenceId, I think...
Thanks, Soon Hui, I fixed the example...
Great news!
Thx.
Post a Comment