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

  • Home
  • SEARCH
  • 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 703035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:47:13+00:00 2026-05-14T03:47:13+00:00

I am relatively new to using MSpec and as I write more and more

  • 0

I am relatively new to using MSpec and as I write more and more tests it becomes obvious to reduce duplication you often have to use a base class for your setup as per Rob Conery’s article

I am happy with using the AssertWasCalled method to verify my expectations, but where do you set up a stub’s return value, I find it useful to set the context in the base class injecting my dependencies but that (I think) means that I need to set my stubs up in the Because delegate which just feels wrong.

Is there a better approach I am missing?

  • 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-05-14T03:47:13+00:00Added an answer on May 14, 2026 at 3:47 am

    The initialization/setup of stubs belongs to the arrange phase. The arrange phase is used to get the system into a known state before you exercise it.

    In MSpec, the arrange phase is performed in Establish fields. For example:

    public class When_the_temperature_threshold_is_reached
    {
        static ITemperatureSensor Sensor;
        static Threshold Threshold;
    
        Establish context = () =>
            {
                Sensor = MockRepository.GenerateStub<ITemperatureSensor>();
                Sensor
                    .Stub(x => x.GetTemperature())
                    .Return(42);
    
                Threshold = new Threshold(Sensor);
            };
    
        Because of = () => Reached = Threshold.IsReached(40);
    
        It should_report_that_the_threshold_was_reached =
            () => Reached.ShouldBeTrue();
    }
    

    When you write more tests using that kind of ITemperatureSensor, you should extract a base class that does complicated or repeated setup.

    public abstract class TemperatureSpecs
    {
        protected static ITemperatureSensor CreateSensorAlwaysReporting(int temperature)
        {
            var sensor = MockRepository.GenerateStub<ITemperatureSensor>();
            sensor
                .Stub(x => x.GetTemperature())
                .Return(temperature);
    
            return sensor;
        }
    }
    
    public class When_the_temperature_threshold_is_reached : TemperatureSpecs
    {
        // Everything else cut for brevity.
        Establish context = () =>
            {
                Sensor = CreateSensorAlwaysReporting(42);
    
                Threshold = new Threshold(Sensor);
            };
    }
    

    This gives you the advantage that you can influence the stub’s return value from the context itself: You do this by keeping as much information as possible local to the context and provide a good name for the “setup” method in the base class.

    There is no need to specifiy or expect anything stub-related in Because. When Because is run, your system should be in a state where it can be exercised without further preparation.

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

Sidebar

Ask A Question

Stats

  • Questions 345k
  • Answers 345k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would start with having him try to install the… May 14, 2026 at 5:52 am
  • Editorial Team
    Editorial Team added an answer You cannot extend a class at runtime. Use an instance… May 14, 2026 at 5:52 am
  • Editorial Team
    Editorial Team added an answer This seems to work: //comment()/following-sibling::*[1]/self::item It looks for immediately following… May 14, 2026 at 5:52 am

Related Questions

I am relatively new to using jQuery and would like to use the load
I am new to using RMI and I am relatively new to using exceptions.
The HTML looks kind of like: <dl> <dt> <img src='Images\Something\expand-close.gif' /> Something 1 </dt>
I am relatively new to WPF, and some things with it are quite foreign
I have run in to a bit of a problem and I have done

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.