Mvcontrib.MVC3.Testhelper UpdateModel NullReferenceExceuption during Unit Test

自闭症网瘾萝莉.ら 提交于 2019-12-09 19:52:43

问题


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

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

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

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

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

Any help, pointers would be fantastic...


回答1:


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

and the test:

// 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());


来源:https://stackoverflow.com/questions/4896695/mvcontrib-mvc3-testhelper-updatemodel-nullreferenceexceuption-during-unit-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!