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

发布于 2024-10-10 23:59:38 字数 865 浏览 5 评论 0原文

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

问题在于,因为会导致空引用异常 - 存储库为空。

建立上的断点被击中(但当我将其放入基类中时则没有),但我猜里面的代码没有运行导致我的错误。

任何帮助都会很棒 - 解释和链接也非常感谢。

[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;
}

Just getting started with MSpec and I can't seem to quite get my first spec to pass. Whilst checking the source code is ideal, I don't really want to spend ages doing that right now.

The problem is that Because causes a null reference exception - the repository is null.

A breakpoint on Establish gets hit (but not when I put it in the base class) but I guess the code inside is not being run causing my error.

Any help would be great - explanations and links also appreciated very much.

[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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

快乐很简单 2024-10-17 23:59:38

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

[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;
    }
}

Your code looks good, although the It seems to miss the lambda operator and parenthesis on ShouldNotBeNull. Does this work for you?

[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;
}

Here's the infrastructure code I used to verify that the context above passes:

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文