Class
Mock

Entry point for setting up and asserting mocks.

Definition

Namespace:Telerik.JustMock

Assembly:Telerik.JustMock.dll

Syntax:

cs-api-definition
public class Mock

Inheritance: objectMock

Constructors

Mock()

Declaration

cs-api-definition
public Mock()

Properties

IsOnDemandEnabled

Gets a value indicating whether the on demand optimization is enabled.

Declaration

cs-api-definition
public static bool IsOnDemandEnabled { get; }

Property Value

bool

True if the On Demand optimization is enabled, otherwise false.

IsProfilerEnabled

Gets a value indicating whether the JustMock profiler is enabled.

Declaration

cs-api-definition
public static bool IsProfilerEnabled { get; }

Property Value

bool

True if the profiler is enabled, otherwise false.

Local

Arrange and assert expectations on language features like C# 7 local functions.

Declaration

cs-api-definition
public static ILocalExpectation Local { get; }

Property Value

ILocalExpectation

NonPublic

Arrange and assert expectations on non-public members.

Declaration

cs-api-definition
public static INonPublicExpectation NonPublic { get; }

Property Value

INonPublicExpectation

Methods

Arrange(Expression<Action>)

Setups the target call to act in a specific way.

Declaration

cs-api-definition
public static ActionExpectation Arrange(Expression<Action> expression)

Parameters

expression

Expression<Action>

Target expression

Returns

ActionExpectation

Reference to ActionExpectation to setup the mock.

Arrange<T, TResult>(Func<TResult>)

Setups the target call to act in a specific way.

Declaration

cs-api-definition
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

cs-api-definition
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

cs-api-definition
public static ActionExpectation Arrange<T>(Action action)

Parameters

action

Action

Target action

Returns

ActionExpectation

Reference to ActionExpectation to setup the mock.

Arrange<T>(T, Action<T>)

Setups the target mock call with user expectation.

Declaration

cs-api-definition
public static ActionExpectation Arrange<T>(T obj, Action<T> action)

Parameters

obj

T

Target instance.

action

Action<T>

Returns

ActionExpectation

Reference to ActionExpectation to setup the mock.

Arrange<TResult>(Expression<Func<TResult>>)

Setups the target mock call with user expectation.

Declaration

cs-api-definition
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

cs-api-definition
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.

csharp
Mock.ArrangeSet(() => foo.MyValue = 10).Throws(new InvalidOperationException());

This will throw InvalidOperationException for when foo.MyValue is set with 10.

Declaration

cs-api-definition
public static ActionExpectation ArrangeSet(Action action)

Parameters

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)

csharp
Mock.ArrangeSet(() => foo.MyValue = 10).Throws(new InvalidOperationException());

This will throw InvalidOperationException for when foo.MyValue is set with 10.

Declaration

cs-api-definition
public static ActionExpectation ArrangeSet<T>(Action action)

Parameters

action

Action

Target action

Returns

ActionExpectation

Reference to ActionExpectation to setup the mock.

Assert(Expression<Action>, Args, Occurs, string)

Asserts the specified call from expression.

Declaration

cs-api-definition
public static void Assert(Expression<Action> expression, Args args, Occurs occurs, string message = null)

Parameters

expression

Expression<Action>

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.

message

string

Assert(Expression<Action>, Args, string)

Asserts the specified call from expression.

Declaration

cs-api-definition
public static void Assert(Expression<Action> expression, Args args, string message = null)

Parameters

expression

Expression<Action>

The action to verify.

args

Args

Specifies to ignore the instance and/or arguments during assertion.

message

string

Assert(Expression<Action>, Occurs, string)

Asserts the specified call from expression.

Declaration

cs-api-definition
public static void Assert(Expression<Action> expression, Occurs occurs, string message = null)

Parameters

expression

Expression<Action>

The action to verify.

occurs

Occurs

Specifies the number of times a mock call should occur.

message

string

Assert(Expression<Action>, string)

Asserts a specific call from expression.

Declaration

cs-api-definition
public static void Assert(Expression<Action> expression, string message = null)

Parameters

expression

Expression<Action>

Action expression defining the action to verify.

message

string

Assert(Type, string)

Asserts all expectation on the given type

Declaration

cs-api-definition
public static void Assert(Type type, string message = null)

Parameters

type

Type

The type which declared the methods to assert.

message

string

Assert<T, TResult>(T, Func<T, TResult>, Occurs, string)

Asserts a specific call from expression.

Declaration

cs-api-definition
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

Occurs

Specifies how many times a call has occurred

message

string

Assert<T, TResult>(T, Func<T, TResult>, string)

Asserts a specific call from expression.

Declaration

cs-api-definition
public static void Assert<T, TResult>(T target, Func<T, TResult> func, string message = null)

Parameters

target

T

Target instance

func

Func<T, TResult>

Contains the target mock call

message

string

Assert<T>(T, string)

Asserts all expected calls that are marked as must or to be occurred a certain number of times.

Declaration

cs-api-definition
public static void Assert<T>(T mocked, string message = null)

Parameters

mocked

T

Target instance

message

string

Assert<T>(string)

Asserts all expectation on the given type

Declaration

cs-api-definition
public static void Assert<T>(string message = null)

Parameters

message

string

Assert<TReturn>(Expression<Func<TReturn>>, Args, Occurs, string)

Asserts the specified call from expression.

Declaration

cs-api-definition
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

Args

Specifies to ignore the instance and/or arguments during assertion.

occurs

Occurs

Specifies the number of times a mock call should occur.

message

string

Assert<TReturn>(Expression<Func<TReturn>>, Args, string)

Asserts a specific call from expression.

Declaration

cs-api-definition
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Args args, string message = null)

Parameters

expression

Expression<Func<TReturn>>

Target expression

args

Args

Assert argument

message

string

Assert<TReturn>(Expression<Func<TReturn>>, Occurs, string)

Asserts a specific call from expression.

Declaration

cs-api-definition
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Occurs occurs, string message = null)

Parameters

expression

Expression<Func<TReturn>>

Target expression

occurs

Occurs

Specifies how many times a call has occurred

message

string

Assert<TReturn>(Expression<Func<TReturn>>, string)

Asserts a specific call from expression.

Declaration

cs-api-definition
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, string message = null)

Parameters

expression

Expression<Func<TReturn>>

Target expression

message

string

AssertAll(string)

Asserts all expected setups in the current context.

Declaration

cs-api-definition
public static void AssertAll(string message = null)

Parameters

message

string

AssertAll<T>(T, string)

Asserts all expected setups.

Declaration

cs-api-definition
public static void AssertAll<T>(T mocked, string message = null)

Parameters

mocked

T

Target instance

message

string

AssertSet(Action, Args, Occurs, string)

Asserts the specific property set operation.

Declaration

cs-api-definition
public static void AssertSet(Action action, Args args, Occurs occurs, string message = null)

Parameters

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.

message

string

AssertSet(Action, Args, string)

Asserts the specific property set operation.

Declaration

cs-api-definition
public static void AssertSet(Action action, Args args, string message = null)

Parameters

action

Action

Action defining the set operation

args

Args

Specifies to ignore the instance and/or arguments during assertion.

message

string

AssertSet(Action, Occurs, string)

Asserts the specific property set operation.

Declaration

cs-api-definition
public static void AssertSet(Action action, Occurs occurs, string message = null)

Parameters

action

Action

Action defining the set operation

occurs

Occurs

Specifies the number of times a mock call should occur.

message

string

AssertSet(Action, string)

Asserts the specific property set operation.

Declaration

cs-api-definition
public static void AssertSet(Action action, string message = null)

Parameters

action

Action

Action defining the set operation

message

string

Create(Type)

Creates a mocked instance from a given type with RecursiveLoose behavior.

Declaration

cs-api-definition
public static object Create(Type target)

Parameters

target

Type

Target to mock

Returns

object

Mock instance

Create(Type, Action<IFluentConfig>)

Creates a mocked instance from a given type.

Declaration

cs-api-definition
public static object Create(Type type, Action<IFluentConfig> settings)

Parameters

type

Type

Target type to mock

settings

Action<IFluentConfig>

Mock settings

Returns

object

Mock instance

Create(Type, Behavior)

Creates a mock instance from a given type.

Declaration

cs-api-definition
public static object Create(Type type, Behavior behavior)

Parameters

type

Type

Mocking type

behavior

Behavior

Specifies behavior of the mock. Default is RecursiveLoose

Returns

object

Mock instance

Create(Type, Behavior, params object[])

Creates a mock instance from a given type.

Declaration

cs-api-definition
public static object Create(Type type, Behavior behavior, params object[] args)

Parameters

type

Type

Mocking type

behavior

Behavior

Specifies behavior of the mock. Default is RecursiveLoose

args

object[]

Constructor arguments

Returns

object

Mock instance

Create(Type, Constructor, Behavior)

Creates a mocked instance from a given type.

Declaration

cs-api-definition
public static object Create(Type type, Constructor constructor, Behavior behavior)

Parameters

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

object

Mock instance

Create(Type, params object[])

Creates a mocked instance from a given type with RecursiveLoose behavior.

Declaration

cs-api-definition
public static object Create(Type target, params object[] args)

Parameters

target

Type

Target to mock

args

object[]

Constructor arguments

Returns

object

Mock instance

Create(string)

Creates a mocked instance from a internal class with RecursiveLoose behavior.

Declaration

cs-api-definition
public static object Create(string fullName)

Parameters

fullName

string

Fully qualified name of the target type.

Returns

object

Mock instance

Create(string, Action<IFluentConfig>)

Creates a mocked instance from an internal class.

Declaration

cs-api-definition
public static object Create(string fullName, Action<IFluentConfig> settings)

Parameters

fullName

string

Fully qualified name of the target type.

settings

Action<IFluentConfig>

Settings for the mock

Returns

object

Mock instance

Create(string, Behavior)

Creates a mocked instance from an internal class.

Declaration

cs-api-definition
public static object Create(string fullName, Behavior behavior)

Parameters

fullName

string

Fully qualified name of the target type.

behavior

Behavior

Specifies behavior of the mock. Default is RecursiveLoose

Returns

object

Mock instance

Create<T>()

Creates a mocked instance from a given type with RecursiveLoose behavior.

Declaration

cs-api-definition
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

cs-api-definition
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

cs-api-definition
public static T Create<T>(Behavior behavior)

Parameters

behavior

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

cs-api-definition
public static T Create<T>(Behavior behavior, params object[] args)

Parameters

behavior

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

cs-api-definition
public static T Create<T>(Constructor constructor)

Parameters

constructor

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

cs-api-definition
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

Create<T>(Expression<Func<T>>)

Create a mocked instance from specified real constructor with RecursiveLoose behavior.

Declaration

cs-api-definition
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

cs-api-definition
public static T Create<T>(Expression<Func<T>> expression, Behavior behavior)

Parameters

expression

Expression<Func<T>>

Target expression for specifying the new type.

behavior

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

cs-api-definition
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

cs-api-definition
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

cs-api-definition
public static int GetTimesCalled(Expression<Action> expression)

Parameters

expression

Expression<Action>

The action to inspect

Returns

int

Number of calls

GetTimesCalled(Expression<Action>, Args)

Returns the number of times the specified member was called.

Declaration

cs-api-definition
public static int GetTimesCalled(Expression<Action> expression, Args args)

Parameters

expression

Expression<Action>

The action to inspect

args

Args

Specifies to ignore the instance and/or arguments during assertion.

Returns

int

Number of calls

GetTimesCalled<TReturn>(Expression<Func<TReturn>>)

Returns the number of times the specified member was called.

Declaration

cs-api-definition
public static int GetTimesCalled<TReturn>(Expression<Func<TReturn>> expression)

Parameters

expression

Expression<Func<TReturn>>

The action to inspect

Returns

int

Number of calls

GetTimesCalled<TReturn>(Expression<Func<TReturn>>, Args)

Returns the number of times the specified member was called.

Declaration

cs-api-definition
public static int GetTimesCalled<TReturn>(Expression<Func<TReturn>> expression, Args args)

Parameters

expression

Expression<Func<TReturn>>

The action to inspect

args

Args

Specifies to ignore the instance and/or arguments during assertion.

Returns

int

Number of calls

GetTimesSetCalled(Action)

Returns the number of times the specified setter or event subscription method was called.

Declaration

cs-api-definition
public static int GetTimesSetCalled(Action action)

Parameters

action

Action

The setter or event subscription method to inspect

Returns

int

Number of calls

GetTimesSetCalled(Action, Args)

Returns the number of times the specified setter or event subscription method was called.

Declaration

cs-api-definition
public static int GetTimesSetCalled(Action action, Args args)

Parameters

action

Action

The setter or event subscription method to inspect

args

Args

Specifies to ignore the instance and/or arguments during assertion.

Returns

int

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

cs-api-definition
public static void Intercept(Type typeToIntercept)

Parameters

typeToIntercept

Type

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

cs-api-definition
public static void Intercept<TTypeToIntercept>()

NotIntercept(Type)

Explicitly disables the interception of the given type by the profiler.

Declaration

cs-api-definition
public static void NotIntercept(Type typeToIntercept)

Parameters

typeToIntercept

Type

The type to intercept

NotIntercept<TTypeToIntercept>()

Explicitly disables the interception of the given type by the profiler.

Declaration

cs-api-definition
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.

Declaration

cs-api-definition
public static void Raise(Action eventExpression, params object[] args)

Parameters

eventExpression

Action

Event expression

args

object[]

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

cs-api-definition
public static void Reset()

SetupStatic(Type)

Setups the target for mocking all static calls.

Declaration

cs-api-definition
public static void SetupStatic(Type staticType)

Parameters

staticType

Type

Static type

SetupStatic(Type, Behavior)

Setups the target for mocking all static calls.

Declaration

cs-api-definition
public static void SetupStatic(Type staticType, Behavior behavior)

Parameters

staticType

Type

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

cs-api-definition
public static void SetupStatic(Type staticType, Behavior behavior, StaticConstructor staticConstructor)

Parameters

staticType

Type

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

cs-api-definition
public static void SetupStatic(Type staticType, StaticConstructor staticConstructor)

Parameters

staticType

Type

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

cs-api-definition
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

cs-api-definition
public static void SetupStatic<T>(Behavior behavior)

Parameters

behavior

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

In this article
DefinitionConstructorsMock()PropertiesIsOnDemandEnabledIsProfilerEnabledLocalNonPublicMethodsArrange(Expression<Action>)Arrange<T, TResult>(Func<TResult>)Arrange<T, TResult>(T, Func<T, TResult>)Arrange<T>(Action)Arrange<T>(T, Action<T>)Arrange<TResult>(Expression<Func<TResult>>)ArrangeLike<T>(T, Expression<Func<T, bool>>)ArrangeSet(Action)ArrangeSet<T>(Action)Assert(Expression<Action>, Args, Occurs, string)Assert(Expression<Action>, Args, string)Assert(Expression<Action>, Occurs, string)Assert(Expression<Action>, string)Assert(Type, string)Assert<T, TResult>(T, Func<T, TResult>, Occurs, string)Assert<T, TResult>(T, Func<T, TResult>, string)Assert<T>(T, string)Assert<T>(string)Assert<TReturn>(Expression<Func<TReturn>>, Args, Occurs, string)Assert<TReturn>(Expression<Func<TReturn>>, Args, string)Assert<TReturn>(Expression<Func<TReturn>>, Occurs, string)Assert<TReturn>(Expression<Func<TReturn>>, string)AssertAll(string)AssertAll<T>(T, string)AssertSet(Action, Args, Occurs, string)AssertSet(Action, Args, string)AssertSet(Action, Occurs, string)AssertSet(Action, string)Create(Type)Create(Type, Action<IFluentConfig>)Create(Type, Behavior)Create(Type, Behavior, params object[])Create(Type, Constructor, Behavior)Create(Type, params object[])Create(string)Create(string, Action<IFluentConfig>)Create(string, Behavior)Create<T>()Create<T>(Action<IFluentConfig<T>>)Create<T>(Behavior)Create<T>(Behavior, params object[])Create<T>(Constructor)Create<T>(Constructor, Behavior)Create<T>(Expression<Func<T>>)Create<T>(Expression<Func<T>>, Behavior)Create<T>(params object[])CreateLike<T>(Expression<Func<T, bool>>)GetTimesCalled(Expression<Action>)GetTimesCalled(Expression<Action>, Args)GetTimesCalled<TReturn>(Expression<Func<TReturn>>)GetTimesCalled<TReturn>(Expression<Func<TReturn>>, Args)GetTimesSetCalled(Action)GetTimesSetCalled(Action, Args)Intercept(Type)Intercept<TTypeToIntercept>()NotIntercept(Type)NotIntercept<TTypeToIntercept>()Raise(Action, params object[])Reset()SetupStatic(Type)SetupStatic(Type, Behavior)SetupStatic(Type, Behavior, StaticConstructor)SetupStatic(Type, StaticConstructor)SetupStatic<T>()SetupStatic<T>(Behavior)
Not finding the help you need?
Contact Support