Class Mock
Entry point for setting up and asserting mocks.
Inheritance
Inherited Members
Namespace: Telerik.JustMock
Assembly: Telerik.JustMock.dll
Syntax
public class Mock
Constructors
Mock()
Declaration
public Mock()
Properties
IsOnDemandEnabled
Gets a value indicating whether the on demand optimization is enabled.
Declaration
public static bool IsOnDemandEnabled { get; }
Property Value
System.Boolean
True if the On Demand optimization is enabled, otherwise false. |
IsProfilerEnabled
Gets a value indicating whether the JustMock profiler is enabled.
Declaration
public static bool IsProfilerEnabled { get; }
Property Value
System.Boolean
True if the profiler is enabled, otherwise false. |
Local
Arrange and assert expectations on language features like C# 7 local functions.
Declaration
public static ILocalExpectation Local { get; }
Property Value
ILocalExpectation
|
NonPublic
Arrange and assert expectations on non-public members.
Declaration
public static INonPublicExpectation NonPublic { get; }
Property Value
INonPublicExpectation
|
Methods
Arrange(Expression<Action>)
Setups the target call to act in a specific way.
Declaration
public static ActionExpectation Arrange(Expression<Action> expression)
Parameters
System.Linq.Expressions.Expression<System.Action>
expression
Target expression |
Returns
ActionExpectation
Reference to ActionExpectation to setup the mock. |
Arrange<T>(T, Action<T>)
Setups the target mock call with user expectation.
Declaration
public static ActionExpectation Arrange<T>(T obj, Action<T> action)
Parameters
T
obj
Target instance. |
System.Action<T>
action
|
Returns
ActionExpectation
Reference to ActionExpectation to setup the mock. |
Type Parameters
T
Target type |
Arrange<T>(Action)
Setups the target mock call with user expectation.
Declaration
public static ActionExpectation Arrange<T>(Action action)
Parameters
System.Action
action
Target action |
Returns
ActionExpectation
Reference to ActionExpectation to setup the mock. |
Type Parameters
T
Target type |
Arrange<TResult>(Expression<Func<TResult>>)
Setups the target mock call with user expectation.
Declaration
public static FuncExpectation<TResult> Arrange<TResult>(Expression<Func<TResult>> expression)
Parameters
System.Linq.Expressions.Expression<System.Func<TResult>>
expression
Provide the target method call |
Returns
FuncExpectation<TResult>
Reference to FuncExpectation<TReturn> to setup the mock. |
Type Parameters
TResult
Return type for the target setup. |
Arrange<T, TResult>(T, Func<T, TResult>)
Setups the target mock call with user expectation.
Declaration
public static FuncExpectation<TResult> Arrange<T, TResult>(T obj, Func<T, TResult> func)
Parameters
T
obj
Target instance. |
System.Func<T, TResult>
func
Expression delegate to the target call |
Returns
FuncExpectation<TResult>
Reference to FuncExpectation<TReturn> to setup the mock. |
Type Parameters
T
Target type |
TResult
Return type for the target setup. |
Arrange<T, TResult>(Func<TResult>)
Setups the target call to act in a specific way.
Declaration
public static FuncExpectation<TResult> Arrange<T, TResult>(Func<TResult> func)
Parameters
System.Func<TResult>
func
Expression delegate to the target call |
Returns
FuncExpectation<TResult>
Reference to FuncExpectation<TReturn> to setup the mock. |
Type Parameters
T
Target type |
TResult
Return type for the target setup. |
ArrangeLike<T>(T, Expression<Func<T, Boolean>>)
Arranges the return values of properties and methods according to the given functional specification.
Declaration
public static void ArrangeLike<T>(T mock, Expression<Func<T, bool>> functionalSpecification)
Parameters
T
mock
The mock on which to make the arrangements. If 'null' then the specification will be applied to all instances. |
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>>
functionalSpecification
The functional specification to apply to this mock. |
Type Parameters
T
Mock type. |
Remarks
See article "Create Mocks By Example" for further information on how to write functional specifications.
ArrangeSet(Action)
Setups target property set operation to act in a specific way. Does not work correctly with OnDemand option enabled.
This will throw InvalidOperationException for when foo.MyValue is set with 10.
Mock.ArrangeSet(() => foo.MyValue = 10).Throws(new InvalidOperationException());
Declaration
public static ActionExpectation ArrangeSet(Action action)
Parameters
System.Action
action
Target action |
Returns
ActionExpectation
Reference to ActionExpectation to setup the mock. |
ArrangeSet<T>(Action)
Setups target property set operation to act in a specific way. Use ArrangeSet<T>(Action)
This will throw InvalidOperationException for when foo.MyValue is set with 10.
Mock.ArrangeSet(() => foo.MyValue = 10).Throws(new InvalidOperationException());
Declaration
public static ActionExpectation ArrangeSet<T>(Action action)
Parameters
System.Action
action
Target action |
Returns
ActionExpectation
Reference to ActionExpectation to setup the mock. |
Type Parameters
T
|
Assert(Expression<Action>, String)
Asserts a specific call from expression.
Declaration
public static void Assert(Expression<Action> expression, string message = null)
Parameters
System.Linq.Expressions.Expression<System.Action>
expression
Action expression defining the action to verify. |
System.String
message
|
Assert(Expression<Action>, Args, String)
Asserts the specified call from expression.
Declaration
public static void Assert(Expression<Action> expression, Args args, string message = null)
Parameters
System.Linq.Expressions.Expression<System.Action>
expression
The action to verify. |
Args
args
Specifies to ignore the instance and/or arguments during assertion. |
System.String
message
|
Assert(Expression<Action>, Args, Occurs, String)
Asserts the specified call from expression.
Declaration
public static void Assert(Expression<Action> expression, Args args, Occurs occurs, string message = null)
Parameters
System.Linq.Expressions.Expression<System.Action>
expression
The action to verify. |
Args
args
Specifies to ignore the instance and/or arguments during assertion. |
Occurs
occurs
Specifies the number of times a mock call should occur. |
System.String
message
|
Assert(Expression<Action>, Occurs, String)
Asserts the specified call from expression.
Declaration
public static void Assert(Expression<Action> expression, Occurs occurs, string message = null)
Parameters
System.Linq.Expressions.Expression<System.Action>
expression
The action to verify. |
Occurs
occurs
Specifies the number of times a mock call should occur. |
System.String
message
|
Assert(Type, String)
Asserts all expectation on the given type
Declaration
public static void Assert(Type type, string message = null)
Parameters
System.Type
type
The type which declared the methods to assert. |
System.String
message
|
Assert<T>(T, String)
Asserts all expected calls that are marked as must or to be occurred a certain number of times.
Declaration
public static void Assert<T>(T mocked, string message = null)
Parameters
T
mocked
Target instance |
System.String
message
|
Type Parameters
T
Target type |
Assert<TReturn>(Expression<Func<TReturn>>, String)
Asserts a specific call from expression.
Declaration
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, string message = null)
Parameters
System.Linq.Expressions.Expression<System.Func<TReturn>>
expression
Target expression |
System.String
message
|
Type Parameters
TReturn
Return type for the assert expression |
Assert<TReturn>(Expression<Func<TReturn>>, Args, String)
Asserts a specific call from expression.
Declaration
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Args args, string message = null)
Parameters
System.Linq.Expressions.Expression<System.Func<TReturn>>
expression
Target expression |
Args
args
Assert argument |
System.String
message
|
Type Parameters
TReturn
Return type for the assert expression |
Assert<TReturn>(Expression<Func<TReturn>>, Args, Occurs, String)
Asserts the specified call from expression.
Declaration
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Args args, Occurs occurs, string message = null)
Parameters
System.Linq.Expressions.Expression<System.Func<TReturn>>
expression
The action to verify. |
Args
args
Specifies to ignore the instance and/or arguments during assertion. |
Occurs
occurs
Specifies the number of times a mock call should occur. |
System.String
message
|
Type Parameters
TReturn
Return type for the target call |
Assert<TReturn>(Expression<Func<TReturn>>, Occurs, String)
Asserts a specific call from expression.
Declaration
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Occurs occurs, string message = null)
Parameters
System.Linq.Expressions.Expression<System.Func<TReturn>>
expression
Target expression |
Occurs
occurs
Specifies how many times a call has occurred |
System.String
message
|
Type Parameters
TReturn
Return type for the target call |
Assert<T>(String)
Asserts all expectation on the given type
Declaration
public static void Assert<T>(string message = null)
Parameters
System.String
message
|
Type Parameters
T
The type which declared the methods to assert. |
Assert<T, TResult>(T, Func<T, TResult>, String)
Asserts a specific call from expression.
Declaration
public static void Assert<T, TResult>(T target, Func<T, TResult> func, string message = null)
Parameters
T
target
Target instance |
System.Func<T, TResult>
func
Contains the target mock call |
System.String
message
|
Type Parameters
T
Target type |
TResult
The type of the return value of the method |
Assert<T, TResult>(T, Func<T, TResult>, Occurs, String)
Asserts a specific call from expression.
Declaration
public static void Assert<T, TResult>(T target, Func<T, TResult> func, Occurs occurs, string message = null)
Parameters
T
target
Target instance |
System.Func<T, TResult>
func
Contains the target mock call |
Occurs
occurs
Specifies how many times a call has occurred |
System.String
message
|
Type Parameters
T
Target type |
TResult
The type of the return value of the method |
AssertAll(String)
Asserts all expected setups in the current context.
Declaration
public static void AssertAll(string message = null)
Parameters
System.String
message
|
AssertAll<T>(T, String)
Asserts all expected setups.
Declaration
public static void AssertAll<T>(T mocked, string message = null)
Parameters
T
mocked
Target instance |
System.String
message
|
Type Parameters
T
Target type |
AssertSet(Action, String)
Asserts the specific property set operation.
Declaration
public static void AssertSet(Action action, string message = null)
Parameters
System.Action
action
Action defining the set operation |
System.String
message
|
AssertSet(Action, Args, String)
Asserts the specific property set operation.
Declaration
public static void AssertSet(Action action, Args args, string message = null)
Parameters
System.Action
action
Action defining the set operation |
Args
args
Specifies to ignore the instance and/or arguments during assertion. |
System.String
message
|
AssertSet(Action, Args, Occurs, String)
Asserts the specific property set operation.
Declaration
public static void AssertSet(Action action, Args args, Occurs occurs, string message = null)
Parameters
System.Action
action
Action defining the set operation |
Args
args
Specifies to ignore the instance and/or arguments during assertion. |
Occurs
occurs
Specifies the number of times a mock call should occur. |
System.String
message
|
AssertSet(Action, Occurs, String)
Asserts the specific property set operation.
Declaration
public static void AssertSet(Action action, Occurs occurs, string message = null)
Parameters
System.Action
action
Action defining the set operation |
Occurs
occurs
Specifies the number of times a mock call should occur. |
System.String
message
|
Create(String)
Creates a mocked instance from a internal class with RecursiveLoose behavior.
Declaration
public static object Create(string fullName)
Parameters
System.String
fullName
Fully qualified name of the target type. |
Returns
System.Object
Mock instance |
Create(String, Action<IFluentConfig>)
Creates a mocked instance from an internal class.
Declaration
public static object Create(string fullName, Action<IFluentConfig> settings)
Parameters
System.String
fullName
Fully qualified name of the target type. |
System.Action<IFluentConfig>
settings
Settings for the mock |
Returns
System.Object
Mock instance |
Create(String, Behavior)
Creates a mocked instance from an internal class.
Declaration
public static object Create(string fullName, Behavior behavior)
Parameters
System.String
fullName
Fully qualified name of the target type. |
Behavior
behavior
Specifies behavior of the mock. Default is RecursiveLoose |
Returns
System.Object
Mock instance |
Create(Type)
Creates a mocked instance from a given type with RecursiveLoose behavior.
Declaration
public static object Create(Type target)
Parameters
System.Type
target
Target to mock |
Returns
System.Object
Mock instance |
Create(Type, Action<IFluentConfig>)
Creates a mocked instance from a given type.
Declaration
public static object Create(Type type, Action<IFluentConfig> settings)
Parameters
System.Type
type
Target type to mock |
System.Action<IFluentConfig>
settings
Mock settings |
Returns
System.Object
Mock instance |
Create(Type, Object[])
Creates a mocked instance from a given type with RecursiveLoose behavior.
Declaration
public static object Create(Type target, params object[] args)
Parameters
System.Type
target
Target to mock |
System.Object[]
args
Constructor arguments |
Returns
System.Object
Mock instance |
Create(Type, Behavior)
Creates a mock instance from a given type.
Declaration
public static object Create(Type type, Behavior behavior)
Parameters
System.Type
type
Mocking type |
Behavior
behavior
Specifies behavior of the mock. Default is RecursiveLoose |
Returns
System.Object
Mock instance |
Create(Type, Behavior, Object[])
Creates a mock instance from a given type.
Declaration
public static object Create(Type type, Behavior behavior, params object[] args)
Parameters
System.Type
type
Mocking type |
Behavior
behavior
Specifies behavior of the mock. Default is RecursiveLoose |
System.Object[]
args
Constructor arguments |
Returns
System.Object
Mock instance |
Create(Type, Constructor, Behavior)
Creates a mocked instance from a given type.
Declaration
public static object Create(Type type, Constructor constructor, Behavior behavior)
Parameters
System.Type
type
Target to mock |
Constructor
constructor
Specifies whether to call the base constructor |
Behavior
behavior
Specifies behavior of the mock. Default is RecursiveLoose |
Returns
System.Object
Mock instance |
Create<T>()
Creates a mocked instance from a given type with RecursiveLoose behavior.
Declaration
public static T Create<T>()
Returns
T
Mock instance |
Type Parameters
T
Type of the mock |
Create<T>(Action<IFluentConfig<T>>)
Creates a mocked instance from settings specified in the lambda.
Declaration
public static T Create<T>(Action<IFluentConfig<T>> settings)
Parameters
System.Action<IFluentConfig<T>>
settings
Specifies mock settings |
Returns
T
Mock instance |
Type Parameters
T
Type of the mock |
Create<T>(Expression<Func<T>>)
Create a mocked instance from specified real constructor with RecursiveLoose behavior.
Declaration
public static T Create<T>(Expression<Func<T>> expression)
Parameters
System.Linq.Expressions.Expression<System.Func<T>>
expression
Target expression for specifying the new type. |
Returns
T
Mock instance |
Type Parameters
T
Target type for the mocked instance |
Create<T>(Expression<Func<T>>, Behavior)
Creates a mocked instance from specified real constructor.
Declaration
public static T Create<T>(Expression<Func<T>> expression, Behavior behavior)
Parameters
System.Linq.Expressions.Expression<System.Func<T>>
expression
Target expression for specifying the new type. |
Behavior
behavior
Specifies behavior of the mock. Default is RecursiveLoose |
Returns
T
Mock instance |
Type Parameters
T
Target type for the mocked instance |
Create<T>(Object[])
Creates a mocked instance from a given type with RecursiveLoose behavior.
Declaration
public static T Create<T>(params object[] args)
Parameters
System.Object[]
args
Constructor arguments |
Returns
T
Mock instance |
Type Parameters
T
Mocked object type. |
Create<T>(Behavior)
Creates a mocked instance from a given type.
Declaration
public static T Create<T>(Behavior behavior)
Parameters
Behavior
behavior
Specifies behavior of the mock. Default is RecursiveLoose |
Returns
T
Mock instance |
Type Parameters
T
Type of the mock |
Create<T>(Behavior, Object[])
Creates a mocked instance from a given type.
Declaration
public static T Create<T>(Behavior behavior, params object[] args)
Parameters
Behavior
behavior
Specifies behavior of the mock. Default is RecursiveLoose |
System.Object[]
args
Constructor arguments |
Returns
T
Mock instance |
Type Parameters
T
Type of the mock |
Create<T>(Constructor)
Creates a mocked instance from a given type with RecursiveLoose behavior.
Declaration
public static T Create<T>(Constructor constructor)
Parameters
Constructor
constructor
Specifies whether to call the base constructor |
Returns
T
Mock instance |
Type Parameters
T
Target type |
Create<T>(Constructor, Behavior)
Creates a mocked instance from a given type.
Declaration
public static T Create<T>(Constructor constructor, Behavior behavior)
Parameters
Constructor
constructor
Specifies whether to call the base constructor |
Behavior
behavior
Specifies behavior of the mock. Default is RecursiveLoose |
Returns
T
Mock instance |
Type Parameters
T
Target type |
CreateLike<T>(Expression<Func<T, Boolean>>)
Creates a mock with RecursiveLoose behavior by parsing the given functional specification.
Declaration
public static T CreateLike<T>(Expression<Func<T, bool>> functionalSpecification)
Parameters
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>>
functionalSpecification
The functional specification to apply to the mock object. |
Returns
T
A mock with the given functional specification. |
Type Parameters
T
Type for the argument. |
Remarks
See article "Create Mocks By Example" for further information on how to write functional specifications.
GetTimesCalled(Expression<Action>)
Returns the number of times the specified member was called.
Declaration
public static int GetTimesCalled(Expression<Action> expression)
Parameters
System.Linq.Expressions.Expression<System.Action>
expression
The action to inspect |
Returns
System.Int32
Number of calls |
GetTimesCalled(Expression<Action>, Args)
Returns the number of times the specified member was called.
Declaration
public static int GetTimesCalled(Expression<Action> expression, Args args)
Parameters
System.Linq.Expressions.Expression<System.Action>
expression
The action to inspect |
Args
args
Specifies to ignore the instance and/or arguments during assertion. |
Returns
System.Int32
Number of calls |
GetTimesCalled<TReturn>(Expression<Func<TReturn>>)
Returns the number of times the specified member was called.
Declaration
public static int GetTimesCalled<TReturn>(Expression<Func<TReturn>> expression)
Parameters
System.Linq.Expressions.Expression<System.Func<TReturn>>
expression
The action to inspect |
Returns
System.Int32
Number of calls |
Type Parameters
TReturn
|
GetTimesCalled<TReturn>(Expression<Func<TReturn>>, Args)
Returns the number of times the specified member was called.
Declaration
public static int GetTimesCalled<TReturn>(Expression<Func<TReturn>> expression, Args args)
Parameters
System.Linq.Expressions.Expression<System.Func<TReturn>>
expression
The action to inspect |
Args
args
Specifies to ignore the instance and/or arguments during assertion. |
Returns
System.Int32
Number of calls |
Type Parameters
TReturn
|
GetTimesSetCalled(Action)
Returns the number of times the specified setter or event subscription method was called.
Declaration
public static int GetTimesSetCalled(Action action)
Parameters
System.Action
action
The setter or event subscription method to inspect |
Returns
System.Int32
Number of calls |
GetTimesSetCalled(Action, Args)
Returns the number of times the specified setter or event subscription method was called.
Declaration
public static int GetTimesSetCalled(Action action, Args args)
Parameters
System.Action
action
The setter or event subscription method to inspect |
Args
args
Specifies to ignore the instance and/or arguments during assertion. |
Returns
System.Int32
Number of calls |
Intercept(Type)
Explicitly enables the interception of the given type by the profiler. Interception is usually enabled implicitly by calls to Create(Type) or Arrange(Expression<Action>). This method is rarely needed in cases where you're trying to arrange setters or raise events on a partial mock.
Declaration
public static void Intercept(Type typeToIntercept)
Parameters
System.Type
typeToIntercept
The type to intercept |
Intercept<TTypeToIntercept>()
Explicitly enables the interception of the given type by the profiler. Interception is usually enabled implicitly by calls to Create(Type) or Arrange(Expression<Action>). This method is rarely needed in cases where you're trying to arrange setters or raise events on a partial mock.
Declaration
public static void Intercept<TTypeToIntercept>()
Type Parameters
TTypeToIntercept
The type to intercept |
NotIntercept(Type)
Explicitly disables the interception of the given type by the profiler.
Declaration
public static void NotIntercept(Type typeToIntercept)
Parameters
System.Type
typeToIntercept
The type to intercept |
NotIntercept<TTypeToIntercept>()
Explicitly disables the interception of the given type by the profiler.
Declaration
public static void NotIntercept<TTypeToIntercept>()
Type Parameters
TTypeToIntercept
The type to intercept |
Raise(Action, Object[])
Raises the specified event. If the event is not mocked and is declared on a C# or VB class and has the default implementation for add/remove, then that event can also be raised using this method, even with the profiler off. The type on which the event is defined may need to be pre-intercepted using Intercept(Type) before calling Raise.
Declaration
public static void Raise(Action eventExpression, params object[] args)
Parameters
System.Action
eventExpression
Event expression |
System.Object[]
args
Delegate arguments |
Reset()
Removes all existing arrangements within the current mocking context (e.g. current test method). Arrangements made in parent mocking contexts (e.g. in fixture setup method) are preserved.
Declaration
public static void Reset()
SetupStatic(Type)
Setups the target for mocking all static calls.
Declaration
public static void SetupStatic(Type staticType)
Parameters
System.Type
staticType
Static type |
SetupStatic(Type, Behavior)
Setups the target for mocking all static calls.
Declaration
public static void SetupStatic(Type staticType, Behavior behavior)
Parameters
System.Type
staticType
Static type |
Behavior
behavior
Specifies behavior of the mock. Default is RecursiveLoose |
SetupStatic(Type, Behavior, StaticConstructor)
Setups the target for mocking all static calls.
Declaration
public static void SetupStatic(Type staticType, Behavior behavior, StaticConstructor staticConstructor)
Parameters
System.Type
staticType
Static type |
Behavior
behavior
Specifies behavior of the mock. Default is RecursiveLoose |
StaticConstructor
staticConstructor
Defines the behavior of the static constructor |
SetupStatic(Type, StaticConstructor)
Setups the target for mocking all static calls.
Declaration
public static void SetupStatic(Type staticType, StaticConstructor staticConstructor)
Parameters
System.Type
staticType
Static type |
StaticConstructor
staticConstructor
Defines the behavior of the static constructor |
SetupStatic<T>()
Setups the target for mocking all static calls with RecursiveLoose behavior.
Declaration
public static void SetupStatic<T>()
Type Parameters
T
Target type |
Remarks
Considers all public members of the class. To mock private member, please use Mock.NonPublic
SetupStatic<T>(Behavior)
Setups the target for mocking all static calls.
Declaration
public static void SetupStatic<T>(Behavior behavior)
Parameters
Behavior
behavior
Specifies behavior of the mock. Default is RecursiveLoose |
Type Parameters
T
Target type |
Remarks
Considers all public members of the class. To mock private member, please use the private interface Mock.NonPublic