Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3672766
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:38:58+00:00 2026-05-19T02:38:58+00:00

Just getting started with MSpec and I can’t seem to quite get my first

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:38:59+00:00Added an answer on May 19, 2026 at 2:38 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There doesn't seem to be any tried and true set of best practices to
(please excuse that I didn't use aliases). I would like my query output to
I'm working on a project in C# w/ XNA, and I want to reorganize
I am playing with TFS 2010, and am trying to setup a build process
My question is about memory use and objects in actionscript 2. If I have
I have a new web app that is packaged as a WAR as part
I am using a 3rd-party rotator object, which is providing a smooth, random rotation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.