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.
Inherited Members
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 SourceBuild()
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. |
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. |
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. |