Show / Hide Table of Contents

Class MockInstanceBuilder<T>

A fluent builder for creating instances of classes with a mix of Moq-based mocks and explicit values for constructor parameters. Use MockUtility.Build<T>() to create an instance of this builder.

Inheritance
object
MockInstanceBuilder<T>
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: Benday.Common.Testing
Assembly: Benday.Common.Testing.dll
Syntax
public class MockInstanceBuilder<T> where T : class
Type Parameters
Name Description
T

The type to create. This is typically the system under test.

Methods

| Edit this page View Source

Build()

Builds the MockCreationResult<T> by resolving the constructor, auto-mocking interface parameters, and applying any explicit values.

Declaration
public MockCreationResult<T> Build()
Returns
Type Description
MockCreationResult<T>

A MockCreationResult containing the lazily-created instance and its mocks.

Exceptions
Type Condition
InvalidOperationException

Thrown when no constructors exist, multiple constructors exist without UsingConstructor, the specified constructor is not found, or a non-mockable parameter has no value provided.

| Edit this page View Source

UsingConstructor(params Type[])

Selects which constructor to use by specifying the parameter types. Required when the target type has multiple constructors.

Declaration
public MockInstanceBuilder<T> UsingConstructor(params Type[] parameterTypes)
Parameters
Type Name Description
Type[] parameterTypes

The types of the constructor parameters, in order.

Returns
Type Description
MockInstanceBuilder<T>

This builder instance for method chaining.

| Edit this page View Source

WithValue<TValue>(TValue)

Provides an explicit value for a constructor parameter by type. Use this for non-mockable types like strings, primitives, and enums. Values are assigned positionally: the first call to WithValue for a given type is assigned to the first constructor parameter of that type, the second call to the second parameter of that type, and so on. Parameters not provided via WithValue will be auto-mocked if they are interfaces.

Declaration
public MockInstanceBuilder<T> WithValue<TValue>(TValue value)
Parameters
Type Name Description
TValue value

The value to pass for this parameter.

Returns
Type Description
MockInstanceBuilder<T>

This builder instance for method chaining.

Type Parameters
Name Description
TValue

The type of the value, matching the constructor parameter type.

Extension Methods

ObjectAssertExtensions.ShouldBeNull<T>(T?, string)
ObjectAssertExtensions.ShouldBeSameAs<T>(T, T, string)
ObjectAssertExtensions.ShouldEqual<T>(T, T, string)
ObjectAssertExtensions.ShouldNotBeNull<T>(T?, string)
ObjectAssertExtensions.ShouldNotBeSameAs<T>(T, T, string)
ObjectAssertExtensions.ShouldNotEqual<T>(T, T, string)
ObjectAssertExtensions.ShouldBeOfType<T>(object?, string)
ObjectAssertExtensions.ShouldNotBeOfType<T>(object?, string)
  • Edit this page
  • View Source
In this article
Back to top Copyright © www.benday.com | info@benday.com