diff --git a/Docs/pages/01-create-mocks.md b/Docs/pages/01-create-mocks.md index 76a28abe..ef083d7e 100644 --- a/Docs/pages/01-create-mocks.md +++ b/Docs/pages/01-create-mocks.md @@ -72,3 +72,26 @@ var sut2 = factory.Create(); Using a factory allows you to create multiple mocks with identical, centrally configured behavior. This is especially useful when you need consistent mock setups across multiple tests or for different types. + +## Wrapping Existing Instances + +You can wrap an existing instance with mock tracking using `Mock.Wrap()`. This allows you to track interactions with +a real object: + +```csharp +var realDispenser = new ChocolateDispenser(); +var wrappedDispenser = Mock.Wrap(realDispenser); + +// Calls are forwarded to the real instance +wrappedDispenser.Dispense("Dark", 5); + +// But you can still verify interactions +wrappedDispenser.VerifyMock.Invoked.Dispense(It.Is("Dark"), It.Is(5)).Once(); +``` + +**Notes:** + +- Only interface types can be wrapped with `Mock.Wrap()`. +- All calls are forwarded to the wrapped instance. +- You can still set up custom behavior that overrides the wrapped instance's behavior. +- Verification works the same as with regular mocks. diff --git a/README.md b/README.md index 183d0a8e..e8d60b1d 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,29 @@ var sut2 = factory.Create(); Using a factory allows you to create multiple mocks with identical, centrally configured behavior. This is especially useful when you need consistent mock setups across multiple tests or for different types. +## Wrapping Existing Instances + +You can wrap an existing instance with mock tracking using `Mock.Wrap()`. This allows you to track interactions with +a real object: + +```csharp +var realDispenser = new ChocolateDispenser(); +var wrappedDispenser = Mock.Wrap(realDispenser); + +// Calls are forwarded to the real instance +wrappedDispenser.Dispense("Dark", 5); + +// But you can still verify interactions +wrappedDispenser.VerifyMock.Invoked.Dispense(It.Is("Dark"), It.Is(5)).Once(); +``` + +**Notes:** + +- Only interface types can be wrapped with `Mock.Wrap()`. +- All calls are forwarded to the wrapped instance. +- You can still set up custom behavior that overrides the wrapped instance's behavior. +- Verification works the same as with regular mocks. + ## Setup Set up return values or behaviors for methods, properties, and indexers on your mock. Control how the mock responds to