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 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

Related Questions

I'm relatively new to Struts2. I've started using ModelDriven to reduce overhead in development.
I am relatively new to using jQuery and I have a problem with creating
I'm relatively new to objective-c...I'm using the iphone 3.0 SDK I have a UIView,
I'm relatively new to .NET and have being using Linq2Sql for a almost a
I'm relatively new to PHP and have been writing a project using what I
I'm relatively new to OpenGL and have been using the GLTools library it provides.
I'm relatively new to gwt and I have a problem using JsArrayInteger. Here is
I'm relatively new to using WPF and the MVVM architecture. I have a question
I'm relatively new to using PHP and have been working on using it to
I'm relatively new to Vim and have been using it without issues so far.

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.