Mock
Provides the main entry point for creating, arranging, and verifying mocks.
Definition
Namespace:Telerik.JustMock
Assembly:Telerik.JustMock.dll
Syntax:
public class Mock
Inheritance: objectMock
Constructors
public Mock()
Properties
Gets helpers that let you arrange and verify language features such as C# local functions.
public static ILocalExpectation Local { get; }
Gets helpers that let you arrange and verify non-public members.
public static INonPublicExpectation NonPublic { get; }
Methods
Arranges a void call by using an expression.
public static ActionExpectation Arrange(Expression<Action> expression)
Expression that identifies the call to arrange.
Returns:Reference to ActionExpectation to setup the mock.
Arranges a return-value call for the specified type.
public static FuncExpectation<TResult> Arrange<T, TResult>(Func<TResult> func)
Delegate that performs the call to arrange.
Returns:FuncExpectation<TResult>
Reference to FuncExpectation to setup the mock.
Arranges a return-value call for a specific instance.
public static FuncExpectation<TResult> Arrange<T, TResult>(T obj, Func<T, TResult> func)
Instance on which the arranged call should occur.
funcFunc<T, TResult>Delegate that performs the call to arrange.
Returns:FuncExpectation<TResult>
Reference to FuncExpectation to setup the mock.
Arranges a void call for the specified type.
public static ActionExpectation Arrange<T>(Action action)
Delegate that performs the call to arrange.
Returns:Reference to ActionExpectation to setup the mock.
Arranges a void call for a specific instance.
public static ActionExpectation Arrange<T>(T obj, Action<T> action)
Instance on which the arranged call should occur.
actionAction<T>Returns:Reference to ActionExpectation to setup the mock.
Arranges a return-value call by using an expression.
public static FuncExpectation<TResult> Arrange<TResult>(Expression<Func<TResult>> expression)
Expression that identifies the call to arrange.
Returns:FuncExpectation<TResult>
Reference to FuncExpectation to setup the mock.
Arranges methods and properties by applying a functional specification.
public static void ArrangeLike<T>(T mock, Expression<Func<T, bool>> functionalSpecification)
Mock on which to apply the specification. If this value is null, JustMock applies the specification to all instances.
functionalSpecificationExpression<Func<T, bool>>Functional specification that describes the arranged behavior.
Use this when you want to express behavior as a specification instead of arranging each member separately.
Arranges a property setter.
Mock.ArrangeSet(() => foo.MyValue = 10).Throws(new InvalidOperationException());
This arrangement throws InvalidOperationException when foo.MyValue is set to 10.
public static ActionExpectation ArrangeSet(Action action)
Setter expression that identifies the property assignment to arrange.
Returns:Reference to ActionExpectation to setup the mock.
This overload is not compatible with On Demand interception.
Arranges a property setter and enables interception for the specified declaring type.
Mock.ArrangeSet(() => foo.MyValue = 10).Throws(new InvalidOperationException());
This arrangement throws InvalidOperationException when foo.MyValue is set to 10.
public static ActionExpectation ArrangeSet<T>(Action action)
Setter expression that identifies the property assignment to arrange.
Returns:Reference to ActionExpectation to setup the mock.
Asserts that the specified void call matches the supplied argument rules and occurrence rule.
public static void Assert(Expression<Action> expression, Args args, Occurs occurs, string message = null)
Expression that identifies the call to verify.
argsArgsSpecifies to ignore the instance and/or arguments during assertion.
occursOccursSpecifies the number of times a mock call should occur.
messagestringCustom failure message.
Asserts that the specified void call occurs by using custom argument matching rules.
public static void Assert(Expression<Action> expression, Args args, string message = null)
Expression that identifies the call to verify.
argsArgsArgument matching rules for the verification.
messagestringCustom failure message.
Asserts that the specified void call satisfies the supplied occurrence rule.
public static void Assert(Expression<Action> expression, Occurs occurs, string message = null)
Expression that identifies the call to verify.
occursOccursSpecifies the number of times a mock call should occur.
messagestringCustom failure message.
Asserts that the specified void call occurs.
public static void Assert(Expression<Action> expression, string message = null)
Expression that identifies the call to verify.
messagestringCustom failure message.
Asserts that the specified return-value call occurs on the supplied instance and satisfies the occurrence rule.
public static void Assert<T, TResult>(T target, Func<T, TResult> func, Occurs occurs, string message = null)
Instance on which the call should occur.
funcFunc<T, TResult>Delegate that performs the call to verify.
occursOccursSpecifies how many times a call has occurred
messagestringCustom failure message.
Asserts that the specified return-value call occurs on the supplied instance.
Asserts all arrangements declared on the specified type.
public static void Assert<T>(string message = null)
Custom failure message.
Asserts all required arrangements for the specified mock instance.
public static void Assert<T>(T mocked, string message = null)
Mock instance to verify.
messagestringCustom failure message.
Asserts that the specified return-value call matches the supplied argument rules and occurrence rule.
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Args args, Occurs occurs, string message = null)
Expression that identifies the call to verify.
argsArgsArgument matching rules for the verification.
occursOccursExpected call-count rule.
messagestringCustom failure message.
Asserts that the specified return-value call occurs by using custom argument matching rules.
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Args args, string message = null)
Expression that identifies the call to verify.
argsArgsArgument matching rules for the verification.
messagestringCustom failure message.
Asserts that the specified return-value call satisfies the supplied occurrence rule.
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Occurs occurs, string message = null)
Expression that identifies the call to verify.
occursOccursExpected call-count rule.
messagestringCustom failure message.
Asserts a specific call from expression.
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, string message = null)
Expression that identifies the call to verify.
messagestringCustom failure message.
Asserts all arrangements in the current mocking context.
public static void AssertAll(string message = null)
Custom failure message.
Asserts all arrangements for the specified mock instance.
public static void AssertAll<T>(T mocked, string message = null)
Mock instance to verify.
messagestringCustom failure message.
Asserts that the specified property setter or event subscription matches the supplied argument rules and occurrence rule.
public static void AssertSet(Action action, Args args, Occurs occurs, string message = null)
Action that identifies the setter or event subscription to verify.
argsArgsSpecifies to ignore the instance and/or arguments during assertion.
occursOccursSpecifies the number of times a mock call should occur.
messagestringCustom failure message.
Asserts that the specified property setter or event subscription occurs by using custom argument matching rules.
Asserts that the specified property setter or event subscription satisfies the supplied occurrence rule.
Clears the recorded invocations for all arrangements on the specified mock instance, without removing or modifying the arrangements themselves. Subsequent calls to Assert(Expression<Action>, string) will count only invocations that occur after this call.
Creates a mocked instance from an internal class.
public static object Create(string fullName, Action<IFluentConfig> settings)
Fully qualified name of the target type.
settingsAction<IFluentConfig>Settings for the mock
Returns:Mock instance
Creates a mocked instance from an internal class.
public static object Create(string fullName, Behavior behavior)
Fully qualified name of the target type.
behaviorBehaviorSpecifies behavior of the mock. Default is RecursiveLoose
Returns:Mock instance
Creates a mocked instance from a internal class with RecursiveLoose behavior.
Creates a mock for the specified runtime type by using fluent configuration settings.
public static object Create(Type type, Action<IFluentConfig> settings)
Type to mock.
settingsAction<IFluentConfig>Delegate that configures mock creation.
Returns:A configured mock instance.
Creates a mock for the specified runtime type by using the supplied behavior and constructor arguments.
public static object Create(Type type, Behavior behavior, params object[] args)
Type to mock.
behaviorBehaviorSpecifies behavior of the mock. Default is RecursiveLoose
argsobject[]Arguments to pass to the constructor when JustMock creates the instance.
Returns:A configured mock instance.
Creates a mock for the specified runtime type by using the supplied behavior.
public static object Create(Type type, Behavior behavior)
Type to mock.
behaviorBehaviorSpecifies behavior of the mock. Default is RecursiveLoose
Returns:A configured mock instance.
Creates a mock for the specified runtime type and controls constructor execution.
public static object Create(Type type, Constructor constructor, Behavior behavior)
Type to mock.
constructorConstructorSpecifies whether JustMock calls the original constructor.
behaviorBehaviorSpecifies behavior of the mock. Default is RecursiveLoose
Returns:A configured mock instance.
Creates a mock for the specified runtime type.
public static object Create(Type target, params object[] args)
Type to mock.
argsobject[]Arguments to pass to the constructor when JustMock creates the instance.
Returns:A mock instance that uses RecursiveLoose behavior.
Creates a mock for the specified runtime type.
public static object Create(Type target)
Type to mock.
Returns:A mock instance that uses RecursiveLoose behavior.
Creates a mock that uses RecursiveLoose behavior.
public static T Create<T>()
T
A mock instance.
Creates a mock by using fluent configuration settings.
public static T Create<T>(Action<IFluentConfig<T>> settings)
Delegate that configures mock creation.
Returns:T
A configured mock instance.
Creates a mock by using the specified behavior and constructor arguments.
public static T Create<T>(Behavior behavior, params object[] args)
Specifies behavior of the mock. Default is RecursiveLoose
argsobject[]Arguments to pass to the constructor when JustMock creates the instance.
Returns:T
A configured mock instance.
Creates a mock by using the specified behavior.
public static T Create<T>(Behavior behavior)
Specifies behavior of the mock. Default is RecursiveLoose
Returns:T
A configured mock instance.
Creates a mock and controls both constructor execution and default behavior.
public static T Create<T>(Constructor constructor, Behavior behavior)
Specifies whether JustMock calls the base constructor.
behaviorBehaviorSpecifies behavior of the mock. Default is RecursiveLoose
Returns:T
A configured mock instance.
Creates a mock and controls whether JustMock calls the instance constructor.
public static T Create<T>(Constructor constructor)
Specifies whether JustMock calls the original constructor.
Returns:T
A mock instance that uses RecursiveLoose behavior.
Creates a mocked instance from specified real constructor.
public static T Create<T>(Expression<Func<T>> expression, Behavior behavior)
Constructor expression that identifies the type and constructor arguments.
behaviorBehaviorSpecifies behavior of the mock. Default is RecursiveLoose
Returns:T
A mock instance for the specified constructor expression.
Create a mocked instance from specified real constructor with RecursiveLoose behavior.
public static T Create<T>(Expression<Func<T>> expression)
Constructor expression that identifies the type and constructor arguments.
Returns:T
A mock instance that uses RecursiveLoose behavior.
Creates a mock that uses RecursiveLoose behavior and the supplied constructor arguments.
public static T Create<T>(params object[] args)
Arguments to pass to the constructor when JustMock creates the instance.
Returns:T
A mock instance.
Creates a mock and arranges it by applying the specified functional specification.
public static T CreateLike<T>(Expression<Func<T, bool>> functionalSpecification)
Functional specification to apply to the mock.
Returns:T
A mock that uses RecursiveLoose behavior.
Use this overload when you want to describe behavior declaratively instead of arranging members one by one.
Returns the number of times the specified member was called.
public static int GetTimesCalled(Expression<Action> expression, Args args)
Expression that identifies the call to inspect.
argsArgsSpecifies to ignore the instance and/or arguments during assertion.
Returns:The number of matching calls.
Returns the number of times the specified member was called.
public static int GetTimesCalled(Expression<Action> expression)
Expression that identifies the call to inspect.
Returns:The number of matching calls.
Returns the number of times the specified member was called.
public static int GetTimesCalled<TReturn>(Expression<Func<TReturn>> expression, Args args)
Expression that identifies the call to inspect.
argsArgsSpecifies to ignore the instance and/or arguments during assertion.
Returns:The number of matching calls.
Returns the number of times the specified member was called.
public static int GetTimesCalled<TReturn>(Expression<Func<TReturn>> expression)
Expression that identifies the call to inspect.
Returns:The number of matching calls.
Returns the number of times the specified setter or event subscription method was called.
Enables profiler interception for the specified type.
public static void Intercept(Type typeToIntercept)
Type to intercept.
JustMock usually enables interception for you when you call Create(Type) or one of the Arrange(Expression<Action>) overloads. Call this method only when you need interception before the first arrangement, for example when you raise events or arrange setters on a partial mock.
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.
public static void Intercept<TTypeToIntercept>()
Disables profiler interception for the specified type.
public static void NotIntercept(Type typeToIntercept)
Type to stop intercepting.
Disables profiler interception for the specified type.
public static void NotIntercept<TTypeToIntercept>()
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.
Removes all arrangements from the current mocking context.
public static void Reset()
Use this method to clear arrangements created in the current test scope without affecting arrangements inherited from parent scopes such as fixture setup methods.
Removes all arrangements and clears all recorded invocations for the specified mock instance. The mock object itself remains usable; new arrangements may be added after this call.
Setups the target for mocking all static calls.
public static void SetupStatic(Type staticType, Behavior behavior, StaticConstructor staticConstructor)
Static type
behaviorBehaviorSpecifies behavior of the mock. Default is RecursiveLoose
staticConstructorStaticConstructorDefines the behavior of the static constructor
Setups the target for mocking all static calls.
public static void SetupStatic(Type staticType, Behavior behavior)
Static type
behaviorBehaviorSpecifies behavior of the mock. Default is RecursiveLoose
Setups the target for mocking all static calls.
public static void SetupStatic(Type staticType, StaticConstructor staticConstructor)
Static type
staticConstructorStaticConstructorDefines the behavior of the static constructor
Setups the target for mocking all static calls.
Setups the target for mocking all static calls with RecursiveLoose behavior.
public static void SetupStatic<T>()
Considers all public members of the class. To mock private member, please use Mock.NonPublic
Setups the target for mocking all static calls.
public static void SetupStatic<T>(Behavior behavior)
Specifies behavior of the mock. Default is RecursiveLoose
Considers all public members of the class. To mock private member, please use the private interface Mock.NonPublic