New to Telerik JustMock? Download free 30-day trial

CallOriginal

Mocks with Behavior.CallOriginal will follow the original implementation of the mocked type for every non explicitly arranged function/member.

Syntax

Mock.Create<T>(Behavior.CallOriginal);

Examples

Basic CallOriginal Example

Assume the following class:

public class Log 
{ 
    public virtual void Info() 
    { 
        throw new NotImplementedException(); 
    } 
} 
Public Class Log 
    Public Overridable Sub Info() 
        Throw New NotImplementedException() 
    End Sub 
End Class 

To show how CallOriginal mocks behave, we have the next example:

[TestMethod] 
[ExpectedException(typeof(NotImplementedException))] 
public void ShouldThrowAnExpectedException() 
{ 
    // Arrange 
    var log = Mock.Create<Log>(Behavior.CallOriginal); 
 
    // Act 
    log.Info(); 
} 
<TestMethod> _ 
<ExpectedException(GetType(NotImplementedException))> _ 
Public Sub ShouldThrowAnExpectedException() 
    ' Arrange 
    Dim log = Mock.Create(Of Log)(Behavior.CallOriginal) 
 
    ' Act 
    log.Info() 
End Sub 

Here, we create a mock of the Log class, with Behavior.CallOriginal and call a not implemented method. This method follows its original logic and throws a NotImplementedException.

Arranging CallOriginal Mock

We are free to further arrange mocks with Behavior.CallOriginal, as shown in the next example:

public class FooBase 
{ 
    public string GetString(string str) 
    { 
        return str; 
    } 
} 
Public Class FooBase 
    Public Function GetString(str As String) As String 
        Return str 
    End Function 
End Class 

[TestMethod] 
public void ShouldAssertAgainstOriginalAndArrangedExpectations() 
{ 
    // Arrange 
    var foo = Mock.Create<FooBase>(Behavior.CallOriginal); 
 
    Mock.Arrange(() => foo.GetString("y")).Returns("z"); 
 
    // Act 
    var actualX = foo.GetString("x"); 
    var actualY = foo.GetString("y"); 
 
    var expectedX = "x"; 
    var expectedY = "z"; 
 
    // Assert 
    Assert.AreEqual(expectedX, actualX); 
    Assert.AreEqual(expectedY, actualY); 
} 
<TestMethod> _ 
Public Sub ShouldAssertAgainstOriginalAndArrangedExpectations() 
    ' Arrange 
    Dim foo = Mock.Create(Of FooBase)(Behavior.CallOriginal) 
 
    Mock.Arrange(Function() foo.GetString("y")).Returns("z") 
 
    ' Act 
    Dim actualX = foo.GetString("x") 
    Dim actualY = foo.GetString("y") 
 
    Dim expectedX = "x" 
    Dim expectedY = "z" 
 
    ' Assert 
    Assert.AreEqual(expectedX, actualX) 
    Assert.AreEqual(expectedY, actualY) 
End Sub 

See Also

In this article