ModelState.IsValid引发了System.IO.FileLoadException类型的异常

时间:2017-02-22 16:45:04

标签: c# .net asp.net-mvc testing nunit

我有一个.NET MVC应用程序,我正在使用NUnit编写一些单元测试。

这是我目前正在测试的功能:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Add(AddInstrumentViewModel model)
{
    if (ModelState.IsValid)
    {
        var group = new FtpServerGroup
        {
            Disabled = true,
            FtpLastPullTime = DateTime.Now,
            FtpPullInterval = TimeSpan.Zero,
        };

        await db.CreateAsync(group, model.Address, model.Username, model.Password);

        // go back to card view
        var res = await db.GetAll();
        return RedirectToAction("Index", res);
    }

    return View(model);
}

这是实际测试:

[Test]
public async Task Test()
{
    var controller = new AutomaticTransferController(mockService.Object);

    var model = new AddInstrumentViewModel
    {
        Address = "124.124.124.124",
        Password = "123456",
        Username = "John Foo"
    };

    var res = await controller.Add(model);
}

使用调试器步进我注意到,只要我输入.Add()方法,行if (ModelState.IsValid)就会抛出异常

  

ModelState.IsValid引发了类型异常   System.IO.FileLoadException

事实上,抛出异常而没有告诉我何时何地

修改

这是我的InstrumentViewModel的代码

    using System.ComponentModel.DataAnnotations;

    public class AddInstrumentViewModel
    {
        [Display(Name = "IP Address or Domain Name")]
        [Required]
        [RegularExpression(@"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$")]
        public string Address { get; set; }

        [Display(Name = "FTP Username")]
        [Required]
        public string Username { get; set; }

        [Display(Name = "FTP Password")]
        [Required]
        public string Password { get; set; }
    }

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。只是更新&amp;巩固nuget包。我不确定究竟是哪一个,因为它们之间存在很多依赖关系。