Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:52:14+00:00 2026-06-10T10:52:14+00:00

I want to substitute object to return sequence of different objects. For example: var

  • 0

I want to substitute object to return sequence of different objects.
For example:

var http = Substitute.For<IHttp>();
http.GetResponse(Arg.Any<string>()).Returns(resourceString, resourceString2);
http.GetResponse(Arg.Any<string>()).Returns(x => { throw new Exception(); });

will return resourceString then resourceString2 then exception.

Or something like this:

var http = Substitute.For<IHttp>();
http.GetResponse(Arg.Any<string>()).Returns(resourceString, x => { throw new Exception(); }, resourceString2);

will return resourceString then exception then resourceString2.

How can I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:52:16+00:00Added an answer on June 10, 2026 at 10:52 am

    This answer is outdated — NSubstitute has direct support for this now. Please see @dangerdex’s answer to this question for more information.


    The multiple returns syntax in NSubstitute only supports values. To also throw exceptions you’ll need to pass a function to Returns, and implement the required logic yourself (e.g. Returns(x => NextValue())).

    There is a related example for Moq sequences on Haacked’s blog using a queue. You can do a similar thing with NSubstitute (example code only, use at your own risk :)):

    public interface IFoo { int Bar(); }
    
    [Test]
    public void Example() {
        var results = new Results<int>(1)
                        .Then(2)
                        .Then(3)
                        .Then(() => { throw new Exception("oops"); });
        var sub = Substitute.For<IFoo>();
        sub.Bar().Returns(x => results.Next());
    
        Assert.AreEqual(1, sub.Bar());
        Assert.AreEqual(2, sub.Bar());
        Assert.AreEqual(3, sub.Bar());
        Assert.Throws<Exception>(() => sub.Bar());
    }
    
    public class Results<T> {
        private readonly Queue<Func<T>> values = new Queue<Func<T>>();
        public Results(T result) { values.Enqueue(() => result); }
        public Results<T> Then(T value) { return Then(() => value); }
        public Results<T> Then(Func<T> value) {
            values.Enqueue(value);
            return this;
        }
        public T Next() { return values.Dequeue()(); }
    }
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to substitute all occurences of string in a file as follows :
I want to substitute a variable in oracle . The var is coming in
Want to run javascript function from parent window in child window Example I have
want to know why String behaves like value type while using ==. String s1
Want to be able to provide a search interface for a collection of objects
I want to substitute all 4 instances of the number 300 from the code
Is it possible to substitute a thing in env var with SED? $ a='aoeua'
I want to substitute, the \ that appears in the Windows directory link to
I want to edit following code.(This code is come from [http://stackoverflow.com/a/9280414/945688] <!DOCTYPE html> <html>
I read a string from a file with shell script variables, and want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.