Skip to content

Instantly share code, notes, and snippets.

@mizrael
Created December 16, 2015 14:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizrael/a061331ff5849bf03bf2 to your computer and use it in GitHub Desktop.
Save mizrael/a061331ff5849bf03bf2 to your computer and use it in GitHub Desktop.
fake implementation of mongodb IFindFluent used for testing
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;
}
}
@makigjuro
Copy link

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 :)

@mizrael
Copy link
Author

mizrael commented Nov 6, 2019

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/

@makigjuro
Copy link

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