NSpec和多个之前

时间:2014-09-06 19:02:10

标签: nspec

我正在玩NSpec并且我与之前的例子混淆:

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;

它有效,但是当我进行下一次更改时:

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;

弦乐声只有" M !!!"。当我调试代码时,它只调用最后一个代码。也许我不理解这个理论,但我相信所有以前的lambdas都会在之前出现过' '行为'而且它是'。这有什么不对?

2 个答案:

答案 0 :(得分:1)

我使用下一个语法并且工作(在方法之前的外部和在上下文中的内部):

    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;

答案 1 :(得分:0)

即使它在前面的例子中增加了 每个规格的再次运行将被休息。

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;