Created
December 16, 2015 14:49
fake implementation of mongodb IFindFluent used for testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FakeFindFluent<TEntity> : IFindFluent<TEntity, TEntity> | |
{ | |
private readonly IEnumerable<TEntity> _items; | |
public FakeFindFluent(IEnumerable<TEntity> items) | |
{ | |
_items = items ?? Enumerable.Empty<TEntity>(); | |
} | |
public Task<TEntity> FirstOrDefaultAsync(CancellationToken cancellationToken) | |
{ | |
return Task.FromResult(_items.FirstOrDefault()); | |
} | |
public IFindFluent<TEntity, TResult> As<TResult>( | |
MongoDB.Bson.Serialization.IBsonSerializer<TResult> resultSerializer = null) | |
{ | |
throw new NotImplementedException(); | |
} | |
public Task<long> CountAsync(CancellationToken cancellationToken) | |
{ | |
return Task.FromResult((long)_items.Count()); | |
} | |
public FilterDefinition<TEntity> Filter | |
{ | |
get { throw new NotImplementedException(); } | |
set { throw new NotImplementedException(); } | |
} | |
public IFindFluent<TEntity, TEntity> Limit(int? limit) | |
{ | |
return this; | |
} | |
public FindOptions<TEntity, TEntity> Options | |
{ | |
get { throw new NotImplementedException(); } | |
} | |
public IFindFluent<TEntity, TNewProjection> Project<TNewProjection>( | |
ProjectionDefinition<TEntity, TNewProjection> projection) | |
{ | |
throw new NotImplementedException(); | |
} | |
public IFindFluent<TEntity, TEntity> Skip(int? skip) | |
{ | |
return this; | |
} | |
public IFindFluent<TEntity, TEntity> Sort(SortDefinition<TEntity> sort) | |
{ | |
return this; | |
} | |
public Task<IAsyncCursor<TEntity>> ToCursorAsync(CancellationToken cancellationToken) | |
{ | |
IAsyncCursor<TEntity> cursor = new FakeAsyncCursor<TEntity>(_items); | |
var task = Task.FromResult(cursor); | |
return task; | |
} | |
} |
hi @makigjuro, thank you very much for the comment! Unfortunately I don't have any example as I've moved to a different approach these days for persistence-related code.
I tend to write integration tests against a temporary database, either dropping it or cleaning it at the end of the suite.
Take a look at this article I wrote: https://www.davideguida.com/handling-integration-tests-in-a-ci-pipeline/
This a log for this answer it means a lot. Actually I found this tool Mongo2go will continue use this and focus more on integration tests.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @mizrael. I find this gist and it is really useful for me. Wanted to ask if you have some example how do you use this together with an Unit Testing framework? Will be grateful :)