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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:17:50+00:00 2026-06-18T02:17:50+00:00

I am studing NSpec framework. Here is my example. I’ve written spec for a

  • 0

I am studing NSpec framework.

Here is my example. I’ve written spec for a simple HttpRequester class:

using Moq;
using NSpec;

namespace FooBrowser.UnitTests.BDD
{
    class HttpRequester_specification : nspec
    {
        private HttpRequester requester;

        private string sentData;
        private int sendTimes;

        private readonly Mock<IConnection> connectionMock;
        private string resource;

        public HttpRequester_specification()
        {
            connectionMock = new Mock<IConnection>();

            connectionMock
                .Setup(x => x.Send(It.IsAny<string>()))
                .Callback<string>(data =>
                {
                    sendTimes++;
                    sentData = data;
                });
        }

        void given_opened_connection_with_no_recent_sends()
        {
            before = () =>
            {
                sendTimes = 0;
            };

            context["when HttpRequester is constructed"] = () =>
            {
                before = () => requester = new HttpRequester(connectionMock.Object);

                it["should not do any request"] = () => sendTimes.should_be(0);

                context["when performing request"] = () =>
                {
                    act = () => requester.Request(resource);

                    context["when resource is not specified"] = () =>
                    {
                        it["should do 1 request"] = () => sendTimes.should_be(1);
                        it["should send HTTP GET / HTTP/1.0"] = () => sentData.should_be("GET / HTTP/1.0");
                    };

                    context["when resource is index.html"] = () =>
                    {
                        before = () => resource = "index.html";

                        it["should do 1 request"] = () => sendTimes.should_be(1);
                        it["should send HTTP GET /index.html HTTP/1.0"] = () => sentData.should_be("GET /index.html HTTP/1.0");
                    };
                };
            };
        }
    }
}

As you can see it[“should do 1 request”] = () => sendTimes.should_be(1); is written twice.

I try to move it to outer context like this:

context["when performing request"] = () =>
{
    act = () => requester.Request(resource);

    context["when resource is not specified"] = () =>
    {
        it["should send HTTP GET / HTTP/1.0"] = () => sentData.should_be("GET / HTTP/1.0");
    };

    context["when resource is index.html"] = () =>
    {
        before = () => resource = "index.html";

        it["should send HTTP GET /index.html HTTP/1.0"] = () => sentData.should_be("GET /index.html HTTP/1.0");
    };

    it["should do 1 request"] = () => sendTimes.should_be(1);
};

But this results to it[“should do 1 request”] = () => sendTimes.should_be(1); is checked once for outer context, not for inner ones as I want.

So, can I move it to outer context somehow?

Or is it easier to contribute some code to NSpec to enable such behavior?

I found similar question here Reusing NSpec specifications but I want to keep lambda-expression syntax (with no inheritance) to see all specs in 1 place.

  • 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-18T02:17:51+00:00Added an answer on June 18, 2026 at 2:17 am

    Sorry to see this is unanswered for two weeks, but I work around it simply by extracting a method like

    void ItShouldRequestExactly(int n)
    {
        it["should do " + n + " request"] = () => sendTimes.should_be(n);
    }
    

    This is DRY enough for me in most cases.
    You get however subtle problems when you pass in objects which are actually initialized at spec execution time, but for this simple example it fits perfectly.
    I sadly don’t see another way to inject such mixin assertions into a context.

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

Sidebar

Related Questions

Studing STL I have written a a simple program to test functors and modifiers.
A friend from college is studing web programming using the tapestry framework, and he
I'm studing Microsoft ASP MVC framework. Here is something strange of my page. I
I am studing java now, and one of my apps is simple Swing file
In this period I am studing the Spring MVC showcase example (downloadable from STS
In this period I am studing the Spring MVC Showcase example dowlodable from the
Hi im studying JSF 2.0 Using tomcat 6.0.26 When a start a simple page
I´m studing the code of OpenCV, and I came across the next few lines:
I start studing iOS last week and before I go any further I would
I'm studing C++ and I can't understand the meaning of the boldface sentence below:

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.