102 lines
7.8 KiB
Markdown
Executable File
102 lines
7.8 KiB
Markdown
Executable File
<!-- include ../../readme.md#content -->
|
|
<!-- #content -->
|
|
The most popular and friendly mocking library for .NET
|
|
|
|
```csharp
|
|
var mock = new Mock<ILoveThisLibrary>();
|
|
|
|
// WOW! No record/replay weirdness?! :)
|
|
mock.Setup(library => library.DownloadExists("2.0.0.0"))
|
|
.Returns(true);
|
|
|
|
// Use the Object property on the mock to get a reference to the object
|
|
// implementing ILoveThisLibrary, and then exercise it by calling
|
|
// methods on it
|
|
ILoveThisLibrary lovable = mock.Object;
|
|
bool download = lovable.DownloadExists("2.0.0.0");
|
|
|
|
// Verify that the given method was indeed called with the expected value at most once
|
|
mock.Verify(library => library.DownloadExists("2.0.0.0"), Times.AtMostOnce());
|
|
```
|
|
|
|
Moq also is the first and only library so far to provide Linq to Mocks, so that the
|
|
same behavior above can be achieved much more succinctly:
|
|
|
|
```csharp
|
|
ILoveThisLibrary lovable = Mock.Of<ILoveThisLibrary>(l =>
|
|
l.DownloadExists("2.0.0.0") == true);
|
|
|
|
// Exercise the instance returned by Mock.Of by calling methods on it...
|
|
bool download = lovable.DownloadExists("2.0.0.0");
|
|
|
|
// Simply assert the returned state:
|
|
Assert.True(download);
|
|
|
|
// If you want to go beyond state testing and want to
|
|
// verify the mock interaction instead...
|
|
Mock.Get(lovable).Verify(library => library.DownloadExists("2.0.0.0"));
|
|
```
|
|
|
|
You can think of Linq to Mocks as "from the universe of mocks, give me one whose behavior
|
|
matches this expression".
|
|
|
|
Check out the [Quickstart](https://github.com/devlooped/moq/wiki/Quickstart) for more examples!
|
|
|
|
<!-- #content -->
|
|
<!-- ../../readme.md#content -->
|
|
<!-- include ../../readme.md#sponsors -->
|
|
<!-- #sponsors -->
|
|
<!-- include https://raw.githubusercontent.com/devlooped/sponsors/main/footer.md -->
|
|
# Sponsors
|
|
|
|
<!-- include sponsors.md -->
|
|
[](https://github.com/clarius)
|
|
[](https://github.com/KirillOsenkov)
|
|
[](https://github.com/MFB-Technologies-Inc)
|
|
[](https://github.com/decriptor)
|
|
[](https://github.com/torutek-gh)
|
|
[](https://github.com/drivenet)
|
|
[](https://github.com/AshleyMedway)
|
|
[](https://github.com/Keflon)
|
|
[](https://github.com/tbolon)
|
|
[](https://github.com/kfrancis)
|
|
[](https://github.com/twenzel)
|
|
[](https://github.com/Giorgi)
|
|
[](https://github.com/unoplatform)
|
|
[](https://github.com/dansiegel)
|
|
[](https://github.com/rbnswartz)
|
|
[](https://github.com/jfoshee)
|
|
[](https://github.com/Mrxx99)
|
|
[](https://github.com/eajhnsn1)
|
|
[](https://github.com/IxTechnologies)
|
|
[](https://github.com/davidjenni)
|
|
[](https://github.com/Jonathan-Hickey)
|
|
[](https://github.com/okyrylchuk)
|
|
[](https://github.com/akunzai)
|
|
[](https://github.com/jakobt)
|
|
[](https://github.com/seanalexander)
|
|
[](https://github.com/tinohager)
|
|
[](https://github.com/ploeh)
|
|
[](https://github.com/KenBonny)
|
|
[](https://github.com/SimonCropp)
|
|
[](https://github.com/agileworks-eu)
|
|
[](https://github.com/sorahex)
|
|
[](https://github.com/arsdragonfly)
|
|
[](https://github.com/vezel-dev)
|
|
[](https://github.com/ChilliCream)
|
|
[](https://github.com/4OTC)
|
|
[](https://github.com/v-limo)
|
|
[](https://github.com/brooke-hamilton)
|
|
|
|
|
|
<!-- sponsors.md -->
|
|
|
|
[](https://github.com/sponsors/devlooped)
|
|
|
|
|
|
[Learn more about GitHub Sponsors](https://github.com/sponsors)
|
|
<!-- https://raw.githubusercontent.com/devlooped/sponsors/main/footer.md -->
|
|
<!-- ../../readme.md#sponsors -->
|
|
|
|
<!-- Exclude from auto-expansion by devlooped/actions-include GH action -->
|
|
<!-- exclude --> |