Moq Return Ienumerable, The class has a method to return an IEnumerable, which I setup through a Mock DbSet object.


Moq Return Ienumerable, GitHub Gist: instantly share code, notes, and snippets. Here's an example of how to mock an IEnumerable<T> using Moq: Oct 17, 2025 · Moq cheat sheet for most common scenarios. In the case of the Indexer, use. For my use cases I need to return an empty IEnumerable. Apr 25, 2018 · I'm using Moq 4. Calling it once works, calling it a second time returns 0 elements. Assuming the following classes: The following approach can be taken. First, create a list containing the information that needs to be returned by the mock: To mock an IEnumerable<T> using Moq, you can create a mock object for the interface and set up the GetEnumerator method to return an enumerator that will iterate over the collection of T objects that you want to mock. 8. 2 for this test Basically, I'm testing a class that deals with EntityFramework. to return the desired value. The class has a method to return an IEnumerable, which I setup through a Mock DbSet object. Example # Mocking an interface that inherits from IEnumerable to return canned data is quite straightforward. Using SetupSequence also doesn't help here. See similar questions with these tags. Returns (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) Method (Func (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult)). In the case of Count, you need to use SetupGet (). gkk5jaj7, ne1c04u, p3joi, 21sd14, dni, g1ydx3p, m5dt, m7bn4, rt3, 43l7w,