BeforeScenario and AfterScenario each being called three times?

901 views
Skip to first unread message

Andy

unread,
Jul 17, 2013, 11:24:36 AM7/17/13
to spec...@googlegroups.com
I was putting together a demo, and the before and after scenario hooks are being called three times each and I can't for the life of me figure out why

I have the base definition

    [Binding]
    public class BaseStepDefinition
    {
        public Calculator calc;

        [BeforeScenario]
        public void Setup()
        {
            calc = new Calculator();
            Console.WriteLine("Before scenario called");
        }

        [AfterScenario]
        public void TearDown()
        {
            calc = null;
            Console.WriteLine("After scenario called");
        }
    }


And two step def classes

    [Binding]
    public class MultiplicationSteps : BaseStepDefinition
    {
        [Given(@"I hit multiply")]
        public void GivenIHitMultiply()
        {
            calc.Multiply();
        }
    }

    [Binding]
    public class AdditionSteps : BaseStepDefinition
    {
        [Given(@"I input the number (\d*)")]
        public void GivenIInputTheNumber(int p0)
        {
            calc.Input(p0);
        }
        // ... a bunch of other methods
    }


and the feature

Feature: Multiplication

Scenario: Multiply two numbers
    Given I input the number 2
    And I hit multiply
    And I input the number 5
    When I press equals
    Then 10 is displayed



and the output is

Before scenario called
Before scenario called
Before scenario called
Given I input the number 2
-> done: AdditionSteps.GivenIInputTheNumber(2) (0.0s)
And I hit multiply
-> done: MultiplicationSteps.GivenIHitMultiply() (0.0s)
And I input the number 5
-> done: AdditionSteps.GivenIInputTheNumber(5) (0.0s)
When I press equals
-> done: AdditionSteps.WhenIPressEquals() (0.0s)
Then 10 is displayed
-> error:   Expected: 10
  But was:  5

After scenario called
After scenario called
After scenario called



Also, each time BeforeScenario is hit, the calc object is null.


I'm really pulling my hair out here, so anything would help.



Andy

unread,
Jul 17, 2013, 11:44:49 AM7/17/13
to spec...@googlegroups.com
It almost looks like its executing it not for every scenario, but for every binding it finds.

If I take the [Binding] tag off the base step definition, it calls them both twice - and in both cases it looks like they're separate instances?

Andy

unread,
Jul 17, 2013, 11:49:01 AM7/17/13
to spec...@googlegroups.com
Ok, fixed it.  The Calculator has to be declared as static and the setup & teardown methods have to be private.
Reply all
Reply to author
Forward
0 new messages