DbSet mock, no results while calling ToList secondly

后端 未结 4 1498
情深已故
情深已故 2020-12-15 06:29

I\'m trying to mock DbContext and DbSet. This works for my previous unit tests, but problem occurs while my code was calling ToList method on DbSet second time.

Firs

4条回答
  •  暖寄归人
    2020-12-15 06:50

    You return the very same enumerator instance upon each call to GetEnumerator. When it enumerates once, it is done, EF doesn't call its Reset method, rather it asks for a new enumerator.

    But you return the one that just has yielded all elements and yields no more.

    Instead, return a function that returns the enumerator, that will return a new enumerator each time you ask for it.

     q.Setup(m => m.GetEnumerator()).Returns( () => queryableData.GetEnumerator() );
    

提交回复
热议问题