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 9126103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:57:37+00:00 2026-06-17T06:57:37+00:00

void ABC() { var foo = Substitute.For<IFoo>(); foo.When(x => x.Bar()).Do(x => counter++); <use Bar()>….

  • 0
void ABC()
{
    var foo = Substitute.For<IFoo>();
    foo.When(x => x.Bar()).Do(x => counter++);
    <use Bar()>.... 1
    foo.When(x => x.Bar()).Do(x => counter--);
    <use Bar()>.... 2
}

For the above code snippet both (1) and (2) are displaying the counter++ behavior indicating that the When…Do behavior is not getting overridden. I need this behavior for generating my testing scenario where I want to hookup different callbacks.

How should I achieve this?

  • 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-17T06:57:39+00:00Added an answer on June 17, 2026 at 6:57 am

    The Do callback does not get replaced, but both should execute. For example (using NSub 1.4.3.0):

    var counter = 0;
    var sub = Substitute.For<IFoo>();
    sub.When(x => x.Bar()).Do(x => counter++);
    sub.Bar();
    Console.WriteLine(counter);  // prints 1
    sub.When(x => x.Bar()).Do(x => counter--);
    sub.Bar();
    Console.WriteLine(counter);  // prints 1, as counter gets inc'd to 2, then dec'd to 1
    

    I suggest that When..Do be used sparingly, as its use can be a symptom of failed encapsulation. Forcing behaviour into a substituted object can indicate that the class we are testing has deep coupling to the behaviour of a dependent class, rather than the interface we are substituting for.

    With that disclaimer, one way you can swap out callbacks is to use a helper class to supply the specific callback:

    [Test]
    public void Example() {
        var counter = 0;
        var helper = new CallbackHelper();
        helper.Callback = x => counter++;
        var sub = Substitute.For<IFoo>();
    
        sub.When(x => x.Bar()).Do(x => helper.Callback(x));
        sub.Bar();
        Console.WriteLine(counter);
    
        helper.Callback = x => counter--;
        sub.Bar();
        Console.WriteLine(counter);
    
        helper.Callback = x => { counter = (counter+1) * 10; };
        sub.Bar();
        Console.WriteLine(counter);
    }
    
    public class CallbackHelper {
        public Action<CallInfo> Callback;
    }
    
    /* Prints:
        1
        0
        10
    */
    

    If you post the specific example of behaviour-swapping you are trying to achieve, we may be able to come up with an interface change to avoid the use of this altogether.

    Hope this helps. 🙂

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

Sidebar

Related Questions

I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
here is the code. void sendMail() { MailMessage mail = new MailMessage(); mail.To.Add(abc@gmail.com); mail.From
I have seen the following code: protected override void Initialize(RequestContext requestContext) { base.Initialize(requestContext); var
This is my code #include <stdio.h> void abc(char *text); int main(void) { char text[20];
Here is my Ajax code: var email=abc@abc.com; $.ajax({ url : ships, data : {email
#include<stdio.h> #include<conio.h> #define ABC 20 #define XYZ 10 #define XXX ABC - XYZ void
protected void Button1_Click(object sender, EventArgs e) { System.Collections.ArrayList list = new System.Collections.ArrayList(); list.Add(abc); list.Add(xyz);
See the following code and please clear my doubts. As ABC is a template,
Given the following two classes: public class ABC { public void Accept(Ordering<User> xyz) {
I have my code organized as following class MyClass { public: typedef void (MyClass::Ptr2func)();

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.