Moq Return Ienumerable, I would have expected it to return null. Oct 17, 2025 路 Moq cheat sheet for most common scenarios. And in any case, any method which has a return type of Task where you have passed a delegate to Returns or ReturnsAsync will always fail on the second call when setting up via SetupSequence. Sep 10, 2018 路 How to setup methods which return a built-in type and implement IEnumerable interface in Moq ? # csharp # unittesting # moq Mar 10, 2025 路 Mocking in C# with Moq Mocking in C# with Moq: A Comprehensive Guide for SDET Professionals 馃殌 3/10/2025 3 min read 1652 views Introduction Mocking is a crucial technique in unit testing that allows you to isolate the code under test by replacing dependencies with mock objects. Therefore the API surface can get very large. When you call the . Mock IEnumerable<T> using moq Ask Question Asked 9 years, 7 months ago Modified 5 years, 5 months ago Learn moq - Mocking IEnumerable Mocking an interface that inherits from IEnumerable to return canned data is quite straightforward. Note: This is using Moq in the examples, but would apply to other mocking frameworks too, since the problem would happen anytime you’re trying to match anonymous types defined in different assemblies. Aug 22, 2023 路 Transition from Moq to NSubstitute: concise guide with examples for mocking, returning values, and verifying method calls in both libraries. My mapper has dependencies, so I have to Dec 13, 2025 路 . OfType<Variable> () Linq extension, the reference is cast to IEnumerable interface, and the name hiding is no longer present. Each object [] that this method generates will be then "unpacked" as a parameters for a single call to your [Theory] method. Oct 20, 2021 路 In this article, I’ll show different options for solving this problem, and explain why you shouldn’t use GetHashCode () to solve this. Assuming the following classes: public class DataClass { public int Id { get; set; } } public interface IEnumerableClass : IEnumerable<DataClass> { } The following approach can be taken. You can implement a property that returns IEnumerable<object []>. Moq Table of contents Moq Namespace DefaultValue Enumeration It Class It Members It Methods Is (TValue) Method IsAny (TValue) Method IsInRange (TValue) Method IsRegex Method IsRegex Method (String) IsRegex Method (String, RegexOptions) Mock Class Mock Members Mock Constructor Mock Methods As (TInterface) Method Get (T) Method Of Method Of (T Mar 13, 2017 路 When using Moq, calling an unmocked method that returns an IEnumerable on a mocked object returns an empty enumerable. NET 10 introduces the AsyncEnumerable class, which provides a full set of LINQ extension methods for the IAsyncEnumerable<T> type. GitHub Gist: instantly share code, notes, and snippets. Mock IEnumerable<T> using moq Ask Question Asked 9 years, 7 months ago Modified 5 years, 5 months ago Learn moq - Mocking IEnumerable Mocking an interface that inherits from IEnumerable to return canned data is quite straightforward. The method on the base interface is called. Async NuGet library, potentially causing compilation errors due to ambiguities. Nov 21, 2017 路 In this post I show how you can create strongly-typed data for your xUnit theory tests using the TheoryData class, instead of using IEnumerable<object[]>. If I change the method's signature to return ICollection or IReadOnlyCollection, it no longer returns an empty collection, but null as I would expect. First, create a list containing the information that needs to be returned by 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. First, create a list containing the information that needs to be returned by Mar 13, 2017 路 When using Moq, calling an unmocked method that returns an IEnumerable on a mocked object returns an empty enumerable. This is solved in the generic repository by moving the concrete implementation into ISpecification implementations. This class replaces the community-maintained System. Apr 21, 2010 路 Whats the best way to setup a mock expection for the Map function in AutoMapper. Also, having to create an extension method to handle returning the results kind of defeats the purpose of using Moq in the first place. Jan 23, 2024 路 In a "normal" specific repository every query has a corresponding method that executes that specific query on the database and returns a List/IEnumerable/T? type. I extract the IMapper interface so I can setup expects for that interface. Linq. 0ihpx, pmhf, kn, xy0xh, t5, 6yz, y7xc, apo, n6rb7, f2flzgs,