简体   繁体   中英

NSpec and multiple befores

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?

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM