Question

Fairly new to TDD; want to give BDD a try first. I'm using an MVP UI Presentation Pattern Framework and I'm trying to write my first test using SubSpec and XUnit but I'm getting a NullReferenceException from my presenter when I make a call to the repo.

I'm sure the answer is obvious, but its got me. Also, it appears that my test is concerned more with the details of the Presentation Pattern -- I trust that it works and probably doesn't need to be tested like below (i.e. raising the view.load event) but I could'nt think of another way. Any suggestions for a better test are welcomed.

My Unit Test:

[Specification]
    public void ViewLoad_WhenTheView.LoadEventIsRaised_ViewLoadShouldGetAll()
    {
        var view = MockRepository.GenerateMock<IOpenJobsView>();
        var repository = MockRepository.GenerateMock<IOpenJobsRepository>();
        var model = new OpenJobsModel().OpenJobs;
        var openJobs = new List<OpenJob>();
        var jobsFromModel = view.Stub(v => v.Model.OpenJobs).Return(model);
        var jobsFromRepo = repository.Stub(r => r.GetAll()).Return(openJobs);
        var presenter = default(OpenJobsPresenter); 

        "Given an openJobsPresenter"
            .Context(() => presenter = new OpenJobsPresenter(view, repository));

        "when the view loads"
            .Do(() => view.Raise(v => v.Load += presenter.ViewLoad, view, new EventArgs()));

        "the object subscribed to the event should result in a call to GetAll"
            .Assert(() => repository.AssertWasCalled(o => o.GetAll()));
        "the results from the call to GetAll should be equal to the model"
            .Assert(() => Assert.Equal(jobsFromModel, jobsFromRepo));

My Presenter:

public class OpenJobsPresenter : Presenter<IOpenJobsView>
{
    readonly IOpenJobsRepository openJobsRepository;

    public OpenJobsPresenter(IOpenJobsView view, IOpenJobsRepository openJobsRepository) : base(view)
    {
        this.openJobsRepository = openJobsRepository;
        View.Load += ViewLoad;
    }

    public void ViewLoad(object sender, System.EventArgs e)
    {
        View.Model.OpenJobs = openJobsRepository.GetAll(); //Getting NullReferenceException here
    }
}
Was it helpful?

Solution

Ok. I was able to figure this out. The null reference was due to the model not being initialized by the view. So I stubbed out the model on the view -- that did it.

I cleaned up my test and included it along with the system under test for a complete solution.

Unit Test:

public class OpenJobsPresenterTests
{
    readonly OpenJobsModel _model;
    readonly IOpenJobsView _view;
    readonly IOpenJobsRepository _repo;

    public OpenJobsPresenterTests()
    {
        _model = MockRepository.GenerateMock<OpenJobsModel>();
        _view = MockRepository.GenerateMock<IOpenJobsView>();
        _repo = MockRepository.GenerateMock<IOpenJobsRepository>();
    }

    [Specification]
    public void OpenJobsPresenterShouldGetAllOpenJobsOnViewLoad()
    {
        var presenter = default(OpenJobsPresenter);
        var openJobs = new List<OpenJob>();
        _view.Stub(v => v.Model).Return(_model);
        _repo.Stub(d => d.GetAll()).Return(openJobs);

        "Given the OpenJobsPresenter"
            .Context(() =>  presenter = new OpenJobsPresenter(_view, _repo));

        "when the view's load event is raised"
            .Do(() => _view.Raise(d => d.Load += presenter.OnViewLoad, _view, new EventArgs()));

        "the event subscriber should get all open jobs"
            .Assert(() => _repo.AssertWasCalled(r => r.GetAll()));

        "the model should equal the results returned"
            .Assert(() => _view.Model.OpenJobs.ShouldBe(openJobs));
    }

}

SUT:

public class OpenJobsPresenter : Presenter<IOpenJobsView>
{
    readonly IOpenJobsRepository openJobsRepository;

    public OpenJobsPresenter(IOpenJobsView view, IOpenJobsRepository openJobsRepository) : base(view)
    {
        this.openJobsRepository = openJobsRepository;
        View.Load += OnViewLoad;
    }

    public void OnViewLoad(object sender, System.EventArgs e)
    {
        View.Model.OpenJobs = openJobsRepository.GetAll();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top