You will need to set up a unit test project first. You will also find runnable examples under the installation directory of Typemock.
Examples:
- Faking Static Methods
- Faking Sealed Types
- Recursive Faking
- Setting Behavior on Chain
- Faking a Singleton
- Faking Live Objects
- Faking Constructors
- Faking Static Constructors
- Verifying Calls
Faking Static Methods
Faking static method is easy with Isolator, here is an example.
The following code acts differently on a leap year
1 2 3 4 5 6 7 |
public static int DoSomethingSpecialOnALeapYear() { if ((DateTime.Now.Month == 2) && (DateTime.Now.Day == 29)) return 100; else return 0; } |
In order to test how this works on a leap year, we need to mock the static method DateTime.Now() here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 |
[TestMethod,Isolated] public void FakingDateTime() { // Arrange - Fake DateTime to think that it is 29th of Feb 2016 Isolate.WhenCalled(() => DateTime.Now).WillReturn(new DateTime(2016, 2, 29)); // Act int result = MyCode.DoSomethingSpecialOnALeapYear(); // Assert Assert.AreEqual(100, result); } |
Faking Sealed Types
Faking sealed types is easy with Isolator, here is an example.
The following code requires an HtppContext in order to work
1 2 3 4 5 6 7 8 9 10 |
public static bool SignOut(HttpContext current) { if (signedIn) { HttpSessionState session = current.Session; session.Abandon(); return true; } return false; } |
In order to test how this works, we need to pass a Fake HttpContext here is an example of how.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[TestMethod,Isolated] public void FakingHttpContext() { // Arrange - Fake an HttpContext var fakeHttp = Isolate.Fake.Instance<HttpContext>(); MyCode.signedIn = true; // Act var result = MyCode.SignOut(fakeHttp); // Assert Assert.AreEqual(true, result); } |
Note this unique feature: a fake instance will automatically fake all methods and chain of methods, called from that instance.
Recursive Faking – Unique Feature
Recursive Faking is the ability to fake Fake everything you don’t care about.
All methods of the fake will return fakes, and they, in turn, will return fakes, and so on.
Doing this with Isolator requires only one line. Here is an example.
The following code requires an HtppContext in order to work
1 2 3 4 5 6 7 8 9 10 |
public static bool SignOut(HttpContext current) { if (signedIn) { HttpSessionState session = current.Session; session.Abandon(); return true; } return false; } |
In order to test how this works, we need to pass a Fake HttpContext and to make sure that Session returns a fake HttpSessionState. here is an example:
1 2 3 4 5 6 7 8 9 10 11 |
[TestMethod,Isolated] public void FakingHttpContext() { // Arrange - Fake an HttpContext var fakeHttp = Isolate.Fake.Instance<HttpContext>(); MyCode.signedIn = true; // Act var result = MyCode.SignOut(fakeHttp); // Assert Assert.AreEqual(true, result); } |
Note: this feature makes our tests robust and eliminates maintenance when production code changes call to the faked object.
Setting behavior on a chain – Unique Feature
Setting behavior on a chain of calls can be done in one call. Here is an example.
The following code requires a Process in order to work
1 2 3 4 5 6 7 8 9 |
public static bool IsMySiteNameTypemock(Process process) { var name = process.MachineName; if (process.MainModule.Site.Name.StartsWith("Typemock")) return true; else return false; } |
In order to test how this works, we need to pass a Fake Process and to simulate its Name. Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[TestMethod,Isolated] public void SetBehaviorOnChainExample() { // Arrange - Fake a Process var fake = Isolate.Fake.Instance<Process>(); // Set Fake Process Name Isolate.WhenCalled(() => fake.MainModule.Site.Name).WillReturn("Typemock rocks"); // Act var result = MyCode.IsMySiteNameTypemock(fake); // Assert Assert.AreEqual(true, result); } |
Faking a Singleton – Unique Feature
Faking a singleton can be tricky as it is set only once in the code, so faking it can affect other tests. Here is the correct way to fake a singleton.
To solve this elegantly, we use 2 statements the first will fake the static constructor, that is the place where the singleton creates the instance. When the singleton has already been created in a previous test, this won’t do anything as the static constructor is called once, but when the singleton has not been created, we won’t create the instance. Typemock will make sure that the static constructor will be run the next time an unfaked singleton is used (Cool 🙂
The other statements will fake the Instance property to return a fake one.
1 2 3 4 5 6 7 8 9 10 11 |
[TestMethod,Isolated] public void ExampleOfFakingASingleton() { // Arrange - Fake a Singleton // Fake the static constructor. Isolate.Fake.StaticConstructor<Singleton>(); // Fake the Instance. var fake = Isolate.WhenCalled(()=> Singleton.Instance).ReturnRecursiveFake(); //... } |
Faking Live Objects – Unique Feature
A live object is a test object which has been instantiated normally. It is possible to modify a live object’s behavior. Here is an example:
The following code requires that CheckSecurity will pass in order to work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Dependency { public virtual void CheckSecurity(string name, string password) { throw new SecurityException(); } } public class ClassUnderTest { public int Calculate(int a, int b, Dependency dependency) { dependency.CheckSecurity("typemock", "rules"); return a + b; } } |
In order to test this, we need to fake CheckSecurity. In this example, we will create a real Dependency instance and fake ONLY the CheckSecurity method.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[TestMethod,Isolated] public void FakeMethod_OnLiveObject() { var dependency = new Dependency(); // Fake the CheckSecurity method only Isolate.WhenCalled(() => dependency.CheckSecurity(null, null)).IgnoreCall(); var classUnderTest = new ClassUnderTest(); var result = classUnderTest.Calculate(1, 2, dependency); Assert.AreEqual(3, result); } |
Faking Constructors – Unique Feature
Faking constructors can be done by faking the next instantiation of a type. Here is an example:
The following code requires that CheckSecurity will pass in order to work
|
In order to test this, we need to fake the Dependency object.
1 2 3 4 5 6 7 8 |
[TestMethod,Isolated] public void FakeConstructor() { // Fake the Dependency constructor var fakeHandle = Isolate.Fake.NextInstance<Dependency>(); var result = ClassUnderTest.Calculate(1, 2); Assert.AreEqual(3, result); } |
Note: when we fake the constructor – the next instance is a fake and all methods are recursively fake.
Faking Static Constructors – Unique Feature
This example will show how to faking static constructors.
1 2 3 4 5 6 7 8 9 |
[TestMethod,Isolated] public void FakeStaticConstructor() { // Fake the Dependency static constructor Isolate.Fake.StaticConstructor<Dependency>(); var result = ClassUnderTest.Calculate(1, 2); Assert.AreEqual(3, result); } // after the this test, the static constructor will be called as normal |
Note: after we fake a static constructor – the type will be in an invalid state, Typemock will ensure that the static constructor will be called when needed.
Verifying Calls – Unique Feature
This example will show how to verify calls to a method when a method doesn’t return an object to assert on.
1 2 3 4 5 6 7 8 9 10 |
public static bool SignOut(HttpContext current) { if (signedIn) { HttpSessionState session = current.Session; session.Abandon(); return true; } return false; } |
Lets test that the session was really abandoned
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[TestMethod,Isolated] public void FakingHttpContext() { // Arrange - Fake an HttpContext var fakeHttp = Isolate.Fake.Instance<HttpContext>(); MyCode.signedIn = true; // Act var result = MyCode.SignOut(fakeHttp); // Assert Isolate.Verify.WasCalledWithAnyArgument(()=>fakeHttp.Session.Abandon()); } |