MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

Cara mengejek antarmuka IFindFluent

Mengambil beberapa inspirasi dari https://gist.github.com/mizrael/a061331ff5849bf03bf2 ini dan implementasi yang diperluas yang berhasil untuk saya. Saya telah membuat implementasi palsu dari antarmuka IFindFluent dan memiliki ketergantungan pada antarmuka IAsyncCursor, jadi saya melakukan implementasi palsu itu juga dan menggunakannya sebagai imbalan dari metode tiruan yang ingin saya atur. Dalam implementasi palsu itu, saya telah menginisialisasi enumerable dan mengembalikannya dalam metode yang saya gunakan. Anda masih bisa lebih kreatif dan bermain-main dengan apa pun yang ingin Anda kembalikan. Sejauh ini ini berhasil untuk saya.

Ini adalah implementasi palsu.

public class FakeFindFluent<TEntity, TProjection> : IFindFluent<TEntity, TEntity>
{
    private readonly IEnumerable<TEntity> _items;

    public FakeFindFluent(IEnumerable<TEntity> items)
    {
        _items = items ?? Enumerable.Empty<TEntity>();
    }

    public FilterDefinition<TEntity> Filter { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

    public FindOptions<TEntity, TEntity> Options => throw new NotImplementedException();

    public IFindFluent<TEntity, TResult> As<TResult>(MongoDB.Bson.Serialization.IBsonSerializer<TResult> resultSerializer = null)
    {
        throw new NotImplementedException();
    }

    public long Count(CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }

    public Task<long> CountAsync(CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }

    public long CountDocuments(CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }

    public Task<long> CountDocumentsAsync(CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }

    public IFindFluent<TEntity, TEntity> Limit(int? limit)
    {
        throw new NotImplementedException();
    }

    public IFindFluent<TEntity, TNewProjection> Project<TNewProjection>(ProjectionDefinition<TEntity, TNewProjection> projection)
    {
        throw new NotImplementedException();
    }

    public IFindFluent<TEntity, TEntity> Skip(int? skip)
    {
        throw new NotImplementedException();
    }

    public IFindFluent<TEntity, TEntity> Sort(SortDefinition<TEntity> sort)
    {
        throw new NotImplementedException();
    }

    public IAsyncCursor<TEntity> ToCursor(CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }

    public Task<IAsyncCursor<TEntity>> ToCursorAsync(CancellationToken cancellationToken = default)
    {
        IAsyncCursor<TEntity> cursor = new FakeAsyncCursor<TEntity>(_items);
        var task = Task.FromResult(cursor);

        return task;
    }
}


public class FakeAsyncCursor<TEntity> : IAsyncCursor<TEntity>
{
    private IEnumerable<TEntity> items;

    public FakeAsyncCursor(IEnumerable<TEntity> items)
    {
        this.items = items;
    }

    public IEnumerable<TEntity> Current => items;

    public void Dispose()
    {
        //throw new NotImplementedException();
    }

    public bool MoveNext(CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }

    public Task<bool> MoveNextAsync(CancellationToken cancellationToken = default)
    {
        return Task.FromResult(false);
    }
}

Inilah cara saya mengatur metode tiruan saya untuk mengembalikan apa yang saya inginkan untuk pengujian unit saya.

mockParticipantRepository
                .Setup(x => x.FindByFilter(It.IsAny<FilterDefinition<Participant>>()))
                .Returns(new FakeFindFluent<Participant, Participant>(participantsByRelation));

Saya harap ini bermanfaat.




  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Akses MongoDB langsung melalui JavaScript

  2. Apakah ada cara untuk memaksa mongodb menyimpan indeks tertentu di ram?

  3. Di mana saya harus meletakkan timeline aktivitas di mongodb, disematkan di pengguna atau secara terpisah?

  4. Bagaimana cara mengembalikan jumlah objek yang diperbarui di mongodb?

  5. Lembar Cheat Kinerja untuk MongoDB