0

我正在玩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!!!”。当我调试代码时,它只调用最后一个。也许我不理解这个理论,但我相信所有之前的 lambda 表达式都在“行为”和“它”之前运行。有什么问题?

4

2 回答 2

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;
于 2014-09-06T23:14:17.470 回答
0

即使它在前面的示例中增加了,每个规范的 before 再次运行也将被休息。

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;
于 2015-11-18T07:46:40.350 回答