Swapping One Object's Implementation With Another
You can swap one object implementation with another by using the Swap.CallsOn(Object obj). When a method is called on the swapped object, it is replaced with a corresponding implementation (a method with the same signature) on the swapping object. This means that the swapped object will start behaving like the object it was swapped with for all methods they have in common.
Syntax
C# Isolate.Swap.CallsOn(Object).WithCallsTo(Object);
VB
Isolate.Swap.CallsOn(logger).WithCallsTo(swapped)
Samples
Sample 1: Swapping a real object
C# [TestMethod] public void SwapLoggerWithSwapped() { // Create the object under test RealLogger logger = new RealLogger(); // Create the object calls will be redirected to TestLogger swapped = new TestLogger(); // Redirect any calls from the object under test to the swapping target Isolate.Swap.CallsOn(logger).WithCallsTo(swapped); // logger.Write() is redirected to TestLogger.Write() which writes to console instead of throwing exception logger.Write("Hello World"); // We can still verify the call to logger.Write() happened Isolate.Verify.WasCalledWithAnyArguments(() => logger.Write("")); } public class RealLogger { public void Write(string toWrite) { //logFile.WriteAllText(logFilePath, toWrite); throw new NotImplementedException(); } } public class TestLogger { public void Write(string toWrite) { Console.WriteLine("RealLogger.Write() was called with {0}", toWrite); } }
VB
<TestMethod()>
Public Sub SwapLoggerWithSwapped()
' Create the object under test
Dim logger As New RealLogger()
' Create the object calls will be redirected to
Dim swapped As New TestLogger()
' Redirect any calls from the object under test to the swapping target
Isolate.Swap.CallsOn(logger).WithCallsTo(swapped)
' logger.Write() is redirected to TestLogger.Write() which writes to console instead of throwing exception
logger.Write("Hello World")
' We can still verify the call to logger.Write() happened
Isolate.Verify.WasCalledWithAnyArguments(Sub() logger.Write(""))
End Sub
Public Class RealLogger
Public Sub Write(toWrite As String)
'logFile.WriteAllText(logFilePath, toWrite);
Throw New NotImplementedException()
End Sub
End Class
Public Class TestLogger
Public Sub Write(toWrite As String)
Console.WriteLine("RealLogger.Write() was called with {0}", toWrite)
End Sub
End Class
Sample 2: Swapping a future faked object
You can also use the CallsOn method when you wish to swap a future faked object.
C# [TestMethod] public void SwapFakedLoggerWithSwapped() { // Create the faked object var loggerHandle = Isolate.Fake.NextInstance<RealLogger>(); // Create the object calls will be redirected to TestLogger swapped = new TestLogger(); // Redirect any calls from the object under test to the swapping target Isolate.Swap.CallsOn(loggerHandle).WithCallsTo(swapped); // loggerHandle.Write() is redirected to TestLogger.Write() which writes to console instead of throwing exception RealLogger fakeLogger = new RealLogger(); fakeLogger.Write("Hello World"); // We can still verify the call to loggerHandle.Write() happened Isolate.Verify.WasCalledWithAnyArguments(() => loggerHandle.Write("")); } public class RealLogger { public void Write(string toWrite) { //logFile.WriteAllText(logFilePath, toWrite); throw new NotImplementedException(); } } public class TestLogger { public void Write(string toWrite) { Console.WriteLine("RealLogger.Write() was called with {0}", toWrite); } }
VB
<TestMethod()>
Public Sub SwapFakedLoggerWithSwapped()
' Create the object under test
Dim loggerHandle = Isolate.Fake.NextInstance(Of RealLogger)()
' Create the object calls will be redirected to
Dim swapped As New TestLogger()
' Redirect any calls from the object under test to the swapping target
Isolate.Swap.CallsOn(loggerHandle).WithCallsTo(swapped)
' loggerHandle.Write() is redirected to TestLogger.Write() which writes to console instead of throwing exception
Dim fakeLogger As New RealLogger()
fakeLogger.Write("Hello World")
' We can still verify the call to loggerHandle.Write() happened
Isolate.Verify.WasCalledWithAnyArguments(Sub() loggerHandle.Write(""))
End Sub
Public Class RealLogger
Public Sub Write(toWrite As String)
'logFile.WriteAllText(logFilePath, toWrite);
Throw New NotImplementedException()
End Sub
End Class
Public Class TestLogger
Public Sub Write(toWrite As String)
Console.WriteLine("RealLogger.Write() was called with {0}", toWrite)
End Sub
End Class