كيفية وحدة اختبار أساليب المزامنة لـ Entity Framework مع بنود متعددة

StackOverflow https://stackoverflow.com//questions/21005331

سؤال

أحاول اختبار وحدة طريقة غير متزامنة في EF 6.0.2 باستخدام Moq 4.0.10827 وقد تم منعي فيما يبدو أنه مشكلة في الإرسال.

ما يلي يعمل بشكل جيد:

public async Task<List<Testimonial>> GetByEventIdAsync(int eventId)
{
    var query = from t in _context.Testimonials
                orderby t.Ordinal
                select t;
    var result = query
        .ToListAsync()
        .ConfigureAwait(false);
    return await result;
}

ولكن الفواصل التالية (أضيفت عبارة حيث)

public async Task<List<Testimonial>> GetByEventIdAsync(int eventId)
{
    var query = from t in _context.Testimonials
                where t.EventId == eventId
                orderby t.Ordinal
                select t;
    var result = query
        .ToListAsync()
        .ConfigureAwait(false);
    return await result;
}

لقد قمت ببعض التصحيح ووجدت ما يلي

var q1 = _context.Testimonials; // Type = Castle.Proxies.IDbSet`1Proxy_1
var q2 = q1.Where(t => t.EventId == eventId); // Type = TestDbAsyncEnumerable`1 (from linked code)
var q3 = q2.OrderBy(o => o.Ordinal); // Type = System.Linq.EnumerableQuery`1

لماذا يختفي الصف المزدوج؟وكيف أصلحه؟

تحديث: لقد وجدت أيضًا أنه إذا قمت بإضافة where العودة وإزالة orderby, ، وهذا يعمل أيضًا، لذا يبدو أن المشكلة تحتوي على عبارات متعددة.

هل كانت مفيدة؟

المحلول

وأخيرا وجدت الجواب هنا.

يجب إضافة IQueryable كما يلي:

public class TestDbAsyncEnumerable<T> 
    : EnumerableQuery<T>, IDbAsyncEnumerable<T>, IQueryable
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top