MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

IFindFluent इंटरफ़ेस का नकल कैसे करें

इससे कुछ प्रेरणा ली https://gist.github.com/mizrael/a061331ff5849bf03bf2 और विस्तारित कार्यान्वयन जो मेरे लिए काम करता है। मैंने IFindFluent इंटरफ़ेस का एक नकली कार्यान्वयन बनाया है और इसकी IAsyncCursor इंटरफ़ेस पर निर्भरता थी, इसलिए मैंने इसका नकली कार्यान्वयन भी किया और नकली विधि के बदले में इसका उपयोग करके मैं स्थापित करना चाहता था। उस नकली कार्यान्वयन में, मैंने एक एन्यूमरेबल को इनिशियलाइज़ किया है और उस विधि को वापस कर दिया है जिसका मैं उपयोग कर रहा हूँ। आप अभी भी अधिक रचनात्मक हो सकते हैं और जो कुछ भी आप वापस करना चाहते हैं उसके आसपास खेल सकते हैं। अब तक इसने मेरे लिए काम किया है।

यहाँ एक नकली कार्यान्वयन है।

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);
    }
}

यहां बताया गया है कि मैं अपने यूनिट परीक्षण के लिए जो चाहता था उसे वापस करने के लिए मैंने अपना नकली तरीका कैसे सेट किया।

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

मुझे आशा है कि यह मददगार है।




  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. सी # मोंगोडीबी प्रतिकृति सेट इश्यू से कनेक्ट करें

  2. ECS Fargate बाइंड माउंट का समर्थन नहीं करता है

  3. MongoDB में तिथि के अनुसार संग्रह कैसे क्रमबद्ध करें?

  4. उल्का में मोंगोडब से फील्ड रिटर्न की सीमा संख्या

  5. यूनिट परीक्षण के लिए मोंगोडब मॉकअप सर्वर को कॉन्फ़िगर और उपयोग करें