https://github.com/tugberkugurlu/mongodb.testing
MongoDB integration testing helper from .NET projects
https://github.com/tugberkugurlu/mongodb.testing
dotnet dotnet-core mongodb random-database testing
Last synced: 11 months ago
JSON representation
MongoDB integration testing helper from .NET projects
- Host: GitHub
- URL: https://github.com/tugberkugurlu/mongodb.testing
- Owner: tugberkugurlu
- License: mit
- Created: 2015-02-13T15:43:45.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-10-21T16:19:11.000Z (over 9 years ago)
- Last Synced: 2023-03-12T01:42:38.476Z (over 3 years ago)
- Topics: dotnet, dotnet-core, mongodb, random-database, testing
- Language: C#
- Homepage:
- Size: 87.9 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## MongoDB.Testing
MongoDB integration testing helper from .NET projects that stands up a server and a random database on the fly.
The library is compatible with .NET 4.5.1 and [.NET Standard](https://docs.microsoft.com/en-us/dotnet/articles/standard/library) 1.6.
## Quick Start
Install the library into your testing project through NuGet:
```
Install-Package MongoDB.Testing -pre
```
Write a `mongod` executable locator like below one for example (which is specific to Windows):
```csharp
public class MongodExeLocator : IMongoExeLocator
{
public string Locate()
{
return @"C:\Program Files\MongoDB\Server\3.0\bin\mongod.exe";
}
}
```
Integrate it into your tests:
```csharp
[Test]
public async Task HasEnoughRating_Should_Throw_InvalidOperationException_When_The_User_Is_Not_Found()
{
using (MongoTestServer server = MongoTestServer.Start(27017, new MongodExeLocator()))
{
// ARRANGE
var collection = server.Database.GetCollection("users");
var service = new MyCounterService(collection);
await collection.InsertOneAsync(new UserEntity
{
Id = ObjectId.GenerateNewId().ToString(),
Name = "foo",
Rating = 23
});
// ACT, ASSERT
Assert.Throws(
() => service.HasEnoughRating(ObjectId.GenerateNewId().ToString()));
}
}
```
`MongoTestServer.Start` will do the following:
- Start a `mongod` instance and expose it through the specified port.
- Creates a randomly named MongoDB database on the started instance and exposes it through the `MongoTestServer` instance returned from `MongoTestServer.Start` method.
- Cleans up the resources, kills the mongod.exe instance when the `MongoTestServer` instance is disposed.