MSpec:让我的第一个规格通过

时间:2011-01-09 16:32:10

标签: c# mspec

刚刚开始使用MSpec,我似乎无法完全通过我的第一个规范。虽然检查源代码是理想的,但我现在并不想花费多少时间来做这件事。

问题是因为导致空引用异常 - 存储库为空。

关于Establish的断点被命中(但是当我把它放在基类中时却没有)但我想里面的代码没有被运行导致我的错误。

任何帮助都会很棒 - 解释和链接也非常受欢迎。

[Subject("Sandwich Repository CRUD")]
public class sandwich_repository_can_save_sandwiches : SandwichRepositoryContext
{
    Establish context = () =>
    {
        sandwich = new Sandwich(ValidSandwichName);
        repository = new SandwichRepository();
    };

    Because of = () => { repository.Save(sandwich); };

    It should_contain_the_created_sandwich = repository.GetSandwichByName(ValidSandwichName).ShouldNotBeNull;
}

public abstract class SandwichRepositoryContext
{
    protected static Sandwich sandwich;
    protected const string ValidSandwichName = "Olive Le Fabulos";
    protected static SandwichRepository repository;
}

1 个答案:

答案 0 :(得分:1)

您的代码看起来不错,尽管It似乎错过ShouldNotBeNull上的lambda运算符和括号。这对你有用吗?

[Subject("Sandwich Repository CRUD")]
public class when_a_sandwich_is_created : SandwichRepositoryContext
{
    Establish context = () =>
    {
        sandwich = new Sandwich(ValidSandwichName);
        repository = new SandwichRepository();
    };

    Because of = () => { repository.Save(sandwich); };

    It should_find_the_created_sandwich =
        () => repository.GetSandwichByName(ValidSandwichName).ShouldNotBeNull();
}

public abstract class SandwichRepositoryContext
{
    protected static Sandwich sandwich;
    protected const string ValidSandwichName = "Olive Le Fabulos";
    protected static SandwichRepository repository;
}

这是我用来验证上述环境通过的基础设施代码:

public class SandwichRepository
{
    Sandwich _saved;

    public void Save(Sandwich sandwich)
    {
        _saved = sandwich;
    }

    public Sandwich GetSandwichByName(string validSandwichName)
    {
        if (_saved.ValidSandwichName == validSandwichName)
            return _saved;

        return null;
    }
}

public class Sandwich
{
    public string ValidSandwichName
    {
        get;
        set;
    }

    public Sandwich(string validSandwichName)
    {
        ValidSandwichName = validSandwichName;
    }
}