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