Section 1 – Instance and Type Method Isolation • Isolation Pattern – The simple test pattern is made of three parts: o Arrange – setting up the test, including all isolation statements o Act – invoking the code under test o Assert – verification of state and interaction
[TestFixture,Isolated]
public class TestClass
{
   [Test]
   public void TestWithMock()
   { 
      // Arrange 
      Isolate...
 
      // Act 
 
      // Assert
      Assert.AreEqual
}
• Create a fake instance . o Every method, property and indexer are faked. The default can be overridden by using Isolate.WhenCalled API.
var fake = Isolate.Fake.Instance<ClassToIsolate>();
• Fake a future instance o The next object of type ClassToIsolate that is created inside the production code will be swapped by the fake one.
var fake = Isolate.Fake.Instance<ClassToIsolate>();
Isolate.Swap.NextInstance<ClassToIsolate>().With(fake);
• Fake all static methods
Isolate.Fake.StaticMethods<TypeToIsolate>();


Section 2 – Faking Methods • Fake a static method
Isolate.WhenCalled(() => MyType.MyMethod()).WillReturn(10);
• Fake an instance method
var fake = Isolate.Fake.Instance<ClassToIsolate>();
Isolate.WhenCalled(() => fake.MyMethod()).WillReturn(10);
• Fake a method of an existing instance
var real = new ClassToIsolate();
Isolate.WhenCalled(() => real.MyMethod()).WillReturn(10);
• Fake a private/internal method o Static method
Isolate.NonPublic.WhenCalled(typeof(ClassToIsolate),"PrivateStaticMethod").WillReturn(10);
o Instance method
Isolate.NonPublic.WhenCalled(fake,"PrivateMethod").WillReturn(10);
• Faking properties o TrueProperties - Faking a property on a fake object is as easy as setting the property to the value you want to return. No actual code in setter and getter is called.
var fake = Isolate.Fake.Instance<ClassToIsolate>();
    fake.Property = 5; //
o Faking read-only properties requires an Isolate.WhenCalled use
Isolate.WhenCalled(() => fake. Property).WillReturn(5);
• Faking indexers o TrueIndexer - Only the indexer with the specified index will return the value
Isolate.WhenCalled(() => fake[“index”]).WillReturn(5);
• Returning different values on each invocation o The last statement is for the 2nd call (in this example) on.
Isolate.WhenCalled(() => MyType.MyMethod()).WillReturn(10);
Isolate.WhenCalled(() => MyType.MyMethod()).WillReturn(5);
• Injecting faults
Isolate.WhenCalled(() => MyType.MyMethod()).WillThrow(new Exception());
• Faking chained calls o Only when the whole chain gets called, the fake value is returned Isolate.WhenCalled(() => MyType.MyMethod().Property.GetValue()).WillReturn(5); • Running custom code instead of the method o "c" contains the reference of the object that the method is invoked on, and its parameters. Isolate.WhenCalled(() => MyType.MyMethod(“”)).DoInstead(c=>(c.Parameters[0] as string).ToLower()) ;

Section 3 – Arguments • Faking based on exact arguments o Only when the arguments match at runtime, the behavior change takes affect Isolate.WhenCalled(() => MyType.MyMethod(5)). WithExactArguments().WillReturn(10); • Faking based on custom check o The user can create custom check code to test the arguments against. When the code is true, the behavior change takes affect. Isolate.WhenCalled((int i) => MyType.MyMethod(i)). ArgumentsMatch(i=>i>5).WillReturn(10); • Faking ref/out arguments o Set the values you want returned inside the method call as its arguments. var fakeReturn = new Data(); Isolate.WhenCalled(() => MyType.MyMethod(ref fakeReturn)).ReturnRecursiveFakes();

Section 3 – Asserting • Asserting a call was made, regardless of arguments Isolate.Verify.WasCalledWithAnyArguments(()=> fake.MyMethod()); • Asserting a call was made with exact arguments o If a method was called, but with other arguments, the test fails. Isolate.Verify.WasCalledWithExactArguments(()=> fake.MyMethod()); • Asserting a call wasn’t made Isolate.Verify.WasNotCalled(()=> fake.MyMethod()); • Asserting private/internal calls were made o The WithArguments clause is optional. Isolate.Verify.NonPublic.WasCalled(fake, "PrivateMethod").WithArguments("abc", 5);


Section 4 – Advanced Patterns • Creating a fake instance that calls original methods o Usually used for creating real objects with private constructors: var fake = Isolate.Fake.Instance( Members.CallOriginal); o For objects with public constructor use: var fake = new ClassToIsolate(); • Fake instance that return nulls or 0. var fake = Isolate.Fake.Instance( Members.ReturnNulls); • Fake a fail-fast instance o If method are called that were not set with Isolate.WhenCalled, the test fails var fake = Isolate.Fake.Instance( Members. MustSpecifyReturnValues); • Creating a fake instance and calling its constructor o The arguments specified are the constructor's arguments var fake = Isolate.Fake.Instance( Members. ReturnRecursiveFakes, ConstructorWillBe.Called, 100, “yes”); • Throw on constructor Isolate.Swap.NextInstance(). ConstructorWillThrow(new Exception()); • Type redirection o Any method called on the real object gets called on the fake one Isolate.Swap.CallsOn(real).WithCallsTo(fake);


Section 5 – Operability • Using Typemock Runner from the command line o Example: Running Typemock Isolator with nunit-gui [TypemockPath]\TMockRunner.exe [NunitPath]\bin\nunit.exe test-assembly Alternative way see Adrian suggestion • Running using MSBuild/TFS
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <TypemockLocation>C:\Program Files\Typemock\Typemock.NET</TypemockLocation>
    <NUnit>"C:\Program Files\NUnit\bin\nunit-console.exe"</NUnit>
  </PropertyGroup>
 
  <Import Project ="$(TypemockLocation)\MSBuildSchema\Typemock.MSBuild.Tasks"/>
  <Target Name="TestWithTypemock">
    <Typemock IsolatorStart/>
    <Exec ContinueOnError="true" Command="$(NUnit) Test.dll"/>
    <Typemock IsolatorStop/>
  </Target>
</Project>
• Running using NANT
<?xml version="1.0"?>
<project name="Typemock Examples" default="test" basedir=".">
   <property name="nunit" value="C:\Program Files\NUnit\bin\nunit-console.exe" />
   <property name="typemock.dir" value="C:\Program Files\Typemock\Typemock Isolator" />
 
   <target name="test">
       <!-- Dynamically load Isolator task. -->
       <loadtasks assembly="${typemock.dir}\Typemock.NAntBuild.dll" />
       <Typemock IsolatorStart/>
          <exec program="${nunit}" failonerror="false">
             <arg value="Tests.dll" />
          </exec>
       <Typemock IsolatorStop/>
   </target>
</project>
</div>


 

Learn More

Promote Typemock

  • Put the Verified with Typemock logo on your Site

    Verified With TypeMock


Comments? Write to Webmaster
Copyright © Typemock Ltd 2004-2008. All Rights Reserved | Legal