单元测试期间Mvcontrib.MVC3.Testhelper的UpdateModel NullReferenceExceuption [英] Mvcontrib.MVC3.Testhelper UpdateModel NullReferenceExceuption during Unit Test

查看:140
本文介绍了单元测试期间Mvcontrib.MVC3.Testhelper的UpdateModel NullReferenceExceuption的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的单位在ASP.NET MVC 3控制器上测试一个编辑操作。

I am trying to unit test an edit action on my controller in ASP.NET MVC 3.

我已经通过的NuGet安装Mvcontrib.MVC3.TestHelper嘲笑我的控制器上下文,但我仍然得到一个NullReferenceException

I have installed Mvcontrib.MVC3.TestHelper via nuget to mock out my controller Context but I am still getting a NullReferenceException

我的code是这样的:

my code looks like this:

 [TestMethod]
    public void it_should_redirect_to_index_after_editing_a_something_successfully()
    {
        var something= new SomeThing
        {
            ID = Guid.NewGuid(),
            CreatedAt = DateTime.Now,
            LastModified = DateTime.Now,
            Owner = "Me",
            Status = "new",
            Title = "my Title",
            Attachments = new List<Attachment>()
        };

        var repo = new FakeRepository();
        var controller = new SomethingsController(repo);
        new TestControllerBuilder().InitializeController(controller);

        var result = controller.Edit(something) as RedirectToRouteResult;
        result.AssertActionRedirect().ToAction<SomethingsController>(x => x.Index());
    }

生产code看起来像这样...

Production code looks like this...

 [HttpPost]
    public ActionResult Edit(SomeThing something)
    {
        if (ModelState.IsValid)
        {
            var _something = _repository.GetDocumentByID(something.ID);
            TryUpdateModel(_something);
            _something.LastModified = DateTime.Now;
            _repository.SaveChanges();
            return RedirectToAction("Index","Somethings");
        }
        return View(something);
    }

即使我使用的UpdateModel或TryUpdateModel它alwas与一个NullReferenceException崩溃......

And even if I use UpdateModel or TryUpdateModel it alwas crashes with a NullReferenceException...

任何帮助,指针将是非常美妙......

Any help, pointers would be fantastic...

推荐答案

下面是你如何才能够着手:

Here's how you could proceed:

public class Something
{
    public Guid ID { get; set; }
    public DateTime LastModified { get; set; }
    public DateTime CreatedAt { get; set; }
    public string Owner { get; set; }
    public string Status { get; set; }
    public string Title { get; set; }
}

public interface ISomeRepository
{
    Something GetDocumentByID(Guid id);
    void SaveChanges();
}

public class HomeController : Controller
{
    private readonly ISomeRepository _repository;
    public HomeController(ISomeRepository repository)
    {
        _repository = repository;
    }

    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Edit(Something something)
    {
        if (ModelState.IsValid)
        {
            var _something = _repository.GetDocumentByID(something.ID);
            TryUpdateModel(_something);
            _something.LastModified = DateTime.Now;
            _repository.SaveChanges();
            return RedirectToAction("Index", "Home");
        }
        return View(something);
    }
}

和测试:

// arrange
var something = new Something
{
    ID = Guid.NewGuid(),
    CreatedAt = DateTime.Now,
    LastModified = DateTime.Now,
    Owner = "Me",
    Status = "new",
    Title = "my Title",
};
var somethingElse = new Something();
var repo = MockRepository.GenerateStub<ISomeRepository>();
var controller = new HomeController(repo);
new TestControllerBuilder().InitializeController(controller);
repo.Stub(x => x.GetDocumentByID(something.ID)).Return(somethingElse);

var formValues = new FormCollection() 
{
    { "Owner", "some owner" },
};
controller.ValueProvider = formValues.ToValueProvider();

// act
var actual = controller.Edit(something);

// assert
repo.AssertWasCalled(x => x.SaveChanges());
actual
    .AssertActionRedirect()
    .ToAction<HomeController>(x => x.Index());

这篇关于单元测试期间Mvcontrib.MVC3.Testhelper的UpdateModel NullReferenceExceuption的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆