







![]() |
Moq |
IMock<(Of <(T>)>)..::.As<(Of <(TInterface>)>) Method |
IMock<(Of <(T>)>) Interface Example See Also Send Feedback |
Adds an interface implementation to the mock,
allowing expectations to be set for it.
Namespace:
Moq
Assembly:
Moq (in Moq.dll) Version: 2.6.1014.1 (2.6.0.0)
Syntax
C# |
---|
IMock<TInterface> As<TInterface>() where TInterface : class |
Type Parameters
- TInterface
- Type of interface to cast the mock to.
Remarks
This method can only be called before the first use
of the mock Object property, at which
point the runtime type has already been generated
and no more interfaces can be added to it.
Also, TInterface must be an interface and not a class, which must be specified when creating the mock instead.
Examples
The following example creates a mock for the main interface
and later adds IDisposable to it to verify
it's called by the consumer code:
CopyC#

var mock = new Mock<IProcessor>(); mock.Expect(x => x.Execute("ping")); // add IDisposable interface var disposable = mock.As<IDisposable>(); disposable.Expect(d => d.Dispose()).Verifiable();
Exceptions
Exception | Condition |
---|---|
InvalidOperationException | The mock type has already been generated by accessing the Object property. |
ArgumentException | The TInterface specified is not an interface. |