老实说,我无法告诉你在 MSpec 中做到这一点的最佳方法。但是在类似情况下使用 MSpec 时,我遇到了类似的类爆炸问题。我不知道你是否曾经尝试过 RSpec。在 RSpec 中,上下文和规范是在可执行代码的范围内构建的。这意味着您可以创建一个数据结构,对其进行迭代,并使用一个代码块创建多个上下文和规范。当您尝试指定基于数学的事物的行为方式(质数因子、井字游戏、国际象棋等)时,这变得特别方便。可以为一组给定值和期望值的每个成员指定单一的行为模式。
这个例子是用 NSpec 编写的,它是一个以 RSpec 为模型的 C# 上下文/规范框架。我故意留下了一个失败的规范。我刚刚深入了解了这个 kata,找到了一个使用迭代的地方。失败的规范迫使您解决幼稚实现的缺点。
这是素数 kata 的另一个示例:http: //nspec.org/#dolambda
输出:
describe Knight
when moving 2 back and 1 left
when a knight at D4 is moved to B3
knight position should be B3
when a knight at C4 is moved to A3
knight position should be A3 - FAILED - String lengths are both 2. Strings differ at index 0., Expected: "A3", But was: "B3", -----------^
**** FAILURES ****
describe Knight. when moving 2 back and 1 left. when a knight at C4 is moved to A3. knight position should be A3.
String lengths are both 2. Strings differ at index 0., Expected: "A3", But was: "B3", -----------^
at ChessSpecs.describe_Knight.<>c__DisplayClass5.<when_moving_2_back_and_1_left>b__4() in c:\Users\matt\Documents\Visual Studio 2010\Projects\ChessSpecs\ChessSpecs\describe_Knight.cs:line 23
2 Examples, 1 Failed, 0 Pending
代码:
using System.Collections.Generic;
using NSpec;
class describe_Knight : nspec
{
void when_moving_2_back_and_1_left()
{
new Each<string,string> {
{"D4", "B3"},
{"C4", "A3"},
}.Do( (start, moveTo) =>
{
context["when a knight at {0} is moved to {1}".With(start,moveTo)] = () =>
{
before = () =>
{
knight = new Knight(start);
knight.Move(moveTo);
};
it["knight position should be {0}".With(moveTo)] = () => knight.Position.should_be(moveTo);
};
});
}
Knight knight;
}
class Knight
{
public Knight(string position)
{
Position = position;
}
public void Move(string position)
{
Position = "B3";
}
public string Position { get; set; }
}