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;
}
Your code looks good, although the
Itseems 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: