0

I'm playing with NSpec and I'm confused with the before example:

void they_are_loud_and_emphatic()
{
    //act runs after all the befores, and before each spec
    //declares a common act (arrange, act, assert) for all subcontexts
    act = () => sound = sound.ToUpper() + "!!!";
    context["given bam"] = () =>
    {
        before = () => sound = "bam";
        it["should be BAM!!!"] = 
            () => sound.should_be("BAM!!!");
    };
}
string sound;

It works, but when I make the next change:

void they_are_loud_and_emphatic()
{
    //act runs after all the befores, and before each spec
    //declares a common act (arrange, act, assert) for all subcontexts
    act = () => sound = sound.ToUpper() + "!!!";
    context["given bam"] = () =>
    {
        before = () => sound = "b";
        before = () => sound += "a";
        before = () => sound += "m";
        it["should be BAM!!!"] = 
            () => sound.should_be("BAM!!!");
    };
}
string sound;

the string sound only has "M!!!". When I debug the code, it only calls the last before. Perhaps I don't understand the theory, but I believed that all befores lambdas run 'before' the 'act' and the 'it'. What is it wrong?

Setar
  • 261
  • 3
  • 10

2 Answers2

1

I use the next syntax and works (external before method and internal in the context):

    void they_are_loud_and_emphatic()
    {
        act = () => sound = sound.ToUpper() + "!!!";
        context["given bam"] = () =>
        {
            before = () =>
            {
                sound = "b";
                sound += "a";
                sound += "m";
            };

            it["should be BAM!!!"] = () => sound.should_be("BAM!!!");
        };
    }

    string sound;
Setar
  • 261
  • 3
  • 10
  • Thank you for the heads up on this nSpec behaviour. I agree with your expectation in the question and would expect 3 x before = () => statements to assemble "bam". Your solution in this answer makes the Spec more readable so this will not be a problem for me. – camelCase Feb 08 '15 at 15:07
0

even though it was incremented in the previous example the before runs again for each spec will be rested.

void they_are_loud_and_emphatic(){
act = () => sound = sound.ToUpper() + "!!!";
 context["given bam"] = () =>
{
before = () => sound = "b";   //now sound is B!!!
before = () => sound += "a";  //now sound is A!!!
before = () => sound += "m";  //now sound is M!!!
it["should be BAM!!!"] = 
() => sound.should_be("BAM!!!");  // when this line is runing ,sound is"M!!!"
};
}
string sound;