0

TDD 相当新;想先试试BDD。我正在使用 MVP UI 演示模式框架,并且正在尝试使用 SubSpec 和 XUnit 编写我的第一个测试,但是当我调用存储库时,我从演示者那里得到了 NullReferenceException。

我确信答案是显而易见的,但它让我明白了。此外,我的测试似乎更关注演示模式的细节——我相信它可以工作并且可能不需要像下面那样进行测试(即引发 view.load 事件)但我想不出另一种方式。欢迎任何关于更好测试的建议。

我的单元测试:

[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));

我的主持人:

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

1 回答 1

0

好的。我能够弄清楚这一点。空引用是由于视图未初始化模型。所以我在视图上删除了模型——做到了。

我清理了我的测试并将其与被测系统一起包含在内,以获得完整的解决方案。

单元测试:

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

}

苏特:

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();
    }
}
于 2011-12-13T18:22:13.320 回答