1

According to MSpec documentation here, Establish should run only once. However, when I debug the Establish is being run once per the It delegate. Here is a simple example:

public class TestExample
{
        Establish arrange = () =>
        {
            a = 5;
        };
        Because of = () => a = a * 2;
        It should_be1 = () => a.ShouldEqual(10);
        It should_be2 = () => a.ShouldBeGreaterThan(9);
        It should_be3 = () => a.ShouldBeLessThan(90);
        private static int a;
}

Should it run once per It delegate? Or once per the class?

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • I'm not able to reproduce with the code above but the code in this question does see sharedContext running twice. http://stackoverflow.com/questions/32010702/inheritied-establish-executed-multiple-times – Andy Aug 14 '15 at 13:20

1 Answers1

1

It's once per context (class) - unless it has a base class in which case the base class's Establish executes first (recursively, as deep as the class inheritance tree).

How are you determining that it is executing multiple times?

For what it's worth, I cannot reproduce this behaviour using MSpec 0.9.0, the ReSharper runner and the exact code from the question. All of the specs execute and the Establish executes once, as expected.

Sprinkling a few Console.WriteLine() as follows:

using System;
using Machine.Specifications;

namespace StackOverflow_26809460
    {
    public class TestExample
        {
        static int a;

        Establish arrange = () =>
            {
            a = 5;
            Console.WriteLine("Establish");
            };

        Because of = () => a = a*2;

        It should_be1 = () =>
            {
            a.ShouldEqual(10);
            Console.WriteLine("should_be1");
            };

        It should_be2 = () =>
            {
            a.ShouldBeGreaterThan(9);
            Console.WriteLine("should_be1");
            };

        It should_be3 = () =>
            {
            a.ShouldBeLessThan(90);
            Console.WriteLine("should_be1");
            };
        }
    }

When I run that in the command line runner, I get:


Specs in StackOverflow_26809460:

TestExample
Establish
should_be1
» should be1
should_be1
» should be2
should_be1
» should be3


Contexts: 1, Specifications: 3, Time: 0.53 seconds

C:\Users\Tim\Projects\StackOverflow\StackOverflow_26809460\bin\Debug>

It looks like a runner issue to me...

Tim Long
  • 13,508
  • 19
  • 79
  • 147
  • I have a breakpoint and the establish is being executed multiple times. Also, if there is a Cleanup delegate, that also gets executed multiple times. – CodingYoshi Nov 07 '14 at 22:02
  • Which version are you using? If you consider this a bug, please report it on GitHub, along with the version and runner used. – Alexander Groß Nov 08 '14 at 11:06
  • @Tim How is it that Andy and I are seeing it run multiple times and not you? I am using [this](https://visualstudiogallery.msdn.microsoft.com/4abcb54b-53b5-4c44-877f-0397556c5c44) runner but I doubt it would have anything to do with the runner. – CodingYoshi Nov 08 '14 at 23:04
  • The VS test adapter is not updated yet to 0.9.0. I'll try to send a PR against that repo. Are you using 0.9.0 in combination with the adapter? – Daniel Marbach Nov 10 '14 at 15:03
  • @DanielMarbach Yes, I am. So are you saying this is an issue with the runner? – CodingYoshi Nov 10 '14 at 16:08
  • Possibly. Because 0.9.0 uses the runner.utility as a layer of indirection between the core and the runner. if the vs runner isn't updated it won't properly work with the latest core – Daniel Marbach Nov 14 '14 at 09:54