MSpec:让我的第一个规范通过
刚刚开始使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码看起来不错,尽管
It
似乎缺少 lambda 运算符和ShouldNotBeNull
上的括号。这对你有用吗?这是我用来验证上面的上下文是否通过的基础设施代码:
Your code looks good, although the
It
seems to miss the lambda operator and parenthesis onShouldNotBeNull
. Does this work for you?Here's the infrastructure code I used to verify that the context above passes: