ClassMock
Entry point for setting up and asserting mocks.
Definition
Namespace:Telerik.JustMock
Assembly:Telerik.JustMock.dll
Syntax:
public class Mock
Inheritance: objectMock
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
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
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
NonPublic
Arrange and assert expectations on non-public members.
Declaration
public static INonPublicExpectation NonPublic { get; }
Property Value
Methods
Arrange(Expression<Action>)
Setups the target call to act in a specific way.
Declaration
public static ActionExpectation Arrange(Expression<Action> expression)
Parameters
expression
Target expression
Returns
Reference to ActionExpectation to setup the mock.
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
func
Func<TResult>
Expression delegate to the target call
Returns
FuncExpectation<TResult>
Reference to FuncExpectation<TReturn> to setup the mock.
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
obj
T
Target instance.
func
Func<T, TResult>
Expression delegate to the target call
Returns
FuncExpectation<TResult>
Reference to FuncExpectation<TReturn> to setup the mock.
Arrange<T>(Action)
Setups the target mock call with user expectation.
Declaration
public static ActionExpectation Arrange<T>(Action action)
Parameters
action
Target action
Returns
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
obj
T
Target instance.
action
Action<T>
Returns
Reference to ActionExpectation to setup the mock.
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
expression
Expression<Func<TResult>>
Provide the target method call
Returns
FuncExpectation<TResult>
Reference to FuncExpectation<TReturn> to setup the mock.
ArrangeLike<T>(T, Expression<Func<T, bool>>)
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
mock
T
The mock on which to make the arrangements. If 'null' then the specification will be applied to all instances.
functionalSpecification
Expression<Func<T, bool>>
The functional specification to apply to this mock.
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.
Mock.ArrangeSet(() => foo.MyValue = 10).Throws(new InvalidOperationException());
This will throw InvalidOperationException for when foo.MyValue is set with 10.
Declaration
public static ActionExpectation ArrangeSet(Action action)
Parameters
action
Target action
Returns
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)
Mock.ArrangeSet(() => foo.MyValue = 10).Throws(new InvalidOperationException());
This will throw InvalidOperationException for when foo.MyValue is set with 10.
Declaration
public static ActionExpectation ArrangeSet<T>(Action action)
Parameters
action
Target action
Returns
Reference to ActionExpectation to setup the mock.
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
expression
The action to verify.
args
Specifies to ignore the instance and/or arguments during assertion.
occurs
Specifies the number of times a mock call should occur.
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
expression
The action to verify.
args
Specifies to ignore the instance and/or arguments during assertion.
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
expression
The action to verify.
occurs
Specifies the number of times a mock call should occur.
message
Assert(Expression<Action>, string)
Asserts a specific call from expression.
Declaration
public static void Assert(Expression<Action> expression, string message = null)
Parameters
expression
Action expression defining the action to verify.
message
Assert(Type, string)
Asserts all expectation on the given type
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
target
T
Target instance
func
Func<T, TResult>
Contains the target mock call
occurs
Specifies how many times a call has occurred
message
Assert<T, TResult>(T, Func<T, TResult>, string)
Asserts a specific call from expression.
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
mocked
T
Target instance
message
Assert<T>(string)
Asserts all expectation on the given type
Declaration
public static void Assert<T>(string message = null)
Parameters
message
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
expression
Expression<Func<TReturn>>
The action to verify.
args
Specifies to ignore the instance and/or arguments during assertion.
occurs
Specifies the number of times a mock call should occur.
message
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
expression
Expression<Func<TReturn>>
Target expression
args
Assert argument
message
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
expression
Expression<Func<TReturn>>
Target expression
occurs
Specifies how many times a call has occurred
message
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
expression
Expression<Func<TReturn>>
Target expression
message
AssertAll(string)
Asserts all expected setups in the current context.
Declaration
public static void AssertAll(string message = null)
Parameters
message
AssertAll<T>(T, string)
Asserts all expected setups.
Declaration
public static void AssertAll<T>(T mocked, string message = null)
Parameters
mocked
T
Target instance
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
action
Action defining the set operation
args
Specifies to ignore the instance and/or arguments during assertion.
occurs
Specifies the number of times a mock call should occur.
message
AssertSet(Action, Args, string)
Asserts the specific property set operation.
AssertSet(Action, Occurs, string)
Asserts the specific property set operation.
AssertSet(Action, string)
Asserts the specific property set operation.
Create(Type)
Creates a mocked instance from a given type with RecursiveLoose behavior.
Create(Type, Action<IFluentConfig>)
Creates a mocked instance from a given type.
Declaration
public static object Create(Type type, Action<IFluentConfig> settings)
Parameters
type
Target type to mock
settings
Mock settings
Returns
Mock instance
Create(Type, Behavior)
Creates a mock instance from a given type.
Declaration
public static object Create(Type type, Behavior behavior)
Parameters
type
Mocking type
behavior
Specifies behavior of the mock. Default is RecursiveLoose
Returns
Mock instance
Create(Type, Behavior, params object[])
Creates a mock instance from a given type.
Declaration
public static object Create(Type type, Behavior behavior, params object[] args)
Parameters
type
Mocking type
behavior
Specifies behavior of the mock. Default is RecursiveLoose
args
object[]
Constructor arguments
Returns
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
type
Target to mock
constructor
Specifies whether to call the base constructor
behavior
Specifies behavior of the mock. Default is RecursiveLoose
Returns
Mock instance
Create(Type, params object[])
Creates a mocked instance from a given type with RecursiveLoose behavior.
Create(string)
Creates a mocked instance from a internal class with RecursiveLoose behavior.
Create(string, Action<IFluentConfig>)
Creates a mocked instance from an internal class.
Declaration
public static object Create(string fullName, Action<IFluentConfig> settings)
Parameters
fullName
Fully qualified name of the target type.
settings
Settings for the mock
Returns
Mock instance
Create(string, Behavior)
Creates a mocked instance from an internal class.
Declaration
public static object Create(string fullName, Behavior behavior)
Parameters
fullName
Fully qualified name of the target type.
behavior
Specifies behavior of the mock. Default is RecursiveLoose
Returns
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
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
settings
Action<IFluentConfig<T>>
Specifies mock settings
Returns
T
Mock instance
Create<T>(Behavior)
Creates a mocked instance from a given type.
Declaration
public static T Create<T>(Behavior behavior)
Parameters
behavior
Specifies behavior of the mock. Default is RecursiveLoose
Returns
T
Mock instance
Create<T>(Behavior, params object[])
Creates a mocked instance from a given type.
Declaration
public static T Create<T>(Behavior behavior, params object[] args)
Parameters
behavior
Specifies behavior of the mock. Default is RecursiveLoose
args
object[]
Constructor arguments
Returns
T
Mock instance
Create<T>(Constructor)
Creates a mocked instance from a given type with RecursiveLoose behavior.
Declaration
public static T Create<T>(Constructor constructor)
Parameters
constructor
Specifies whether to call the base constructor
Returns
T
Mock instance
Create<T>(Constructor, Behavior)
Creates a mocked instance from a given type.
Declaration
public static T Create<T>(Constructor constructor, Behavior behavior)
Parameters
constructor
Specifies whether to call the base constructor
behavior
Specifies behavior of the mock. Default is RecursiveLoose
Returns
T
Mock instance
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
expression
Expression<Func<T>>
Target expression for specifying the new type.
Returns
T
Mock 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
expression
Expression<Func<T>>
Target expression for specifying the new type.
behavior
Specifies behavior of the mock. Default is RecursiveLoose
Returns
T
Mock instance
Create<T>(params object[])
Creates a mocked instance from a given type with RecursiveLoose behavior.
Declaration
public static T Create<T>(params object[] args)
Parameters
args
object[]
Constructor arguments
Returns
T
Mock instance
CreateLike<T>(Expression<Func<T, bool>>)
Creates a mock with RecursiveLoose behavior by parsing the given functional specification.
Declaration
public static T CreateLike<T>(Expression<Func<T, bool>> functionalSpecification)
Parameters
functionalSpecification
Expression<Func<T, bool>>
The functional specification to apply to the mock object.
Returns
T
A mock with the given functional specification.
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
expression
The action to inspect
Returns
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
expression
The action to inspect
args
Specifies to ignore the instance and/or arguments during assertion.
Returns
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
expression
Expression<Func<TReturn>>
The action to inspect
Returns
Number of calls
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
expression
Expression<Func<TReturn>>
The action to inspect
args
Specifies to ignore the instance and/or arguments during assertion.
Returns
Number of calls
GetTimesSetCalled(Action)
Returns the number of times the specified setter or event subscription method was called.
GetTimesSetCalled(Action, Args)
Returns the number of times the specified setter or event subscription method was called.
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
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>()
NotIntercept(Type)
Explicitly disables the interception of the given type by the profiler.
Declaration
public static void NotIntercept(Type typeToIntercept)
Parameters
typeToIntercept
The type to intercept
NotIntercept<TTypeToIntercept>()
Explicitly disables the interception of the given type by the profiler.
Declaration
public static void NotIntercept<TTypeToIntercept>()
Raise(Action, params 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.
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
staticType
Static type
SetupStatic(Type, Behavior)
Setups the target for mocking all static calls.
Declaration
public static void SetupStatic(Type staticType, Behavior behavior)
Parameters
staticType
Static type
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
staticType
Static type
behavior
Specifies behavior of the mock. Default is RecursiveLoose
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
staticType
Static type
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>()
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
Specifies behavior of the mock. Default is RecursiveLoose
Remarks
Considers all public members of the class. To mock private member, please use the private interface Mock.NonPublic