© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
2 replies
sneezey

❔ NSubstitute on IEnumerable

Hey - I'm trying to "freeze" an
IEnumerable<>
IEnumerable<>
and return a mocked enumerator

However, when doinmg so I get
[Test, AutoData]
public void Test1(
    [Frozen] IEnumerable<int> ints)
{
    // Arrange
    ints
        .GetEnumerator()
        .Returns(new List<int> { 1,2,3,4 }.GetEnumerator());

    // Act

    // Assert
}
[Test, AutoData]
public void Test1(
    [Frozen] IEnumerable<int> ints)
{
    // Arrange
    ints
        .GetEnumerator()
        .Returns(new List<int> { 1,2,3,4 }.GetEnumerator());

    // Act

    // Assert
}


throws
NSubstitute.Exceptions.CouldNotSetReturnDueToNoLastCallException : Could not find a call to return from.

Make sure you called Returns() after calling your substitute (for example: mySub.SomeMethod().Returns(value)),
and that you are not configuring other substitutes within Returns() (for example, avoid this: mySub.SomeMethod().Returns(ConfigOtherSub())).

If you substituted for a class rather than an interface, check that the call to your substitute was on a virtual/abstract member.
Return values cannot be configured for non-virtual/non-abstract members.

Correct use:
    mySub.SomeMethod().Returns(returnValue);

Potentially problematic use:
    mySub.SomeMethod().Returns(ConfigOtherSub());
Instead try:
    var returnValue = ConfigOtherSub();
    mySub.SomeMethod().Returns(returnValue);


  Stack Trace: 
    SubstitutionContext.LastCallShouldReturn(IReturn value, MatchArgs matchArgs)
    IHateTDD.Test1(IEnumerable`1 ints) line 15
NSubstitute.Exceptions.CouldNotSetReturnDueToNoLastCallException : Could not find a call to return from.

Make sure you called Returns() after calling your substitute (for example: mySub.SomeMethod().Returns(value)),
and that you are not configuring other substitutes within Returns() (for example, avoid this: mySub.SomeMethod().Returns(ConfigOtherSub())).

If you substituted for a class rather than an interface, check that the call to your substitute was on a virtual/abstract member.
Return values cannot be configured for non-virtual/non-abstract members.

Correct use:
    mySub.SomeMethod().Returns(returnValue);

Potentially problematic use:
    mySub.SomeMethod().Returns(ConfigOtherSub());
Instead try:
    var returnValue = ConfigOtherSub();
    mySub.SomeMethod().Returns(returnValue);


  Stack Trace: 
    SubstitutionContext.LastCallShouldReturn(IReturn value, MatchArgs matchArgs)
    IHateTDD.Test1(IEnumerable`1 ints) line 15
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

IEnumerable<Author>
C#CC# / help
3mo ago
❔ Need guidance on generics, List, IEnumerable
C#CC# / help
3y ago
IEnumerable and loops
C#CC# / help
6mo ago