【问题标题】:Creating an instance of HttpPostedFileBase for unit testing为单元测试创​​建一个 HttpPostedFileBase 实例
【发布时间】:2014-06-02 13:59:17
【问题描述】:

我需要创建一个HttpPostedFileBase 类对象的实例并将它传递给一个方法,但是我找不到任何方法来实例化它。我正在创建一个测试用例来测试我的 fileupload 方法。

这是我采用HttpPostedFileBase 对象的方法。我需要从我的测试用例类中调用它。我没有使用任何模拟库。

有没有简单的方法来做到这一点?

[HttpPost]
public JsonResult AddVariation(HttpPostedFileBase file, string name, string comment, string description, decimal amount, string accountLineTypeID)
{
    var accountLineType = _fileService.GetAccountLineType(AccountLineType.Debit);
    if (Guid.Parse(accountLineTypeID) == _fileService.GetAccountLineType(AccountLineType.Credit).AccountLineTypeID)
    {
        amount = 0 - amount;
    }
    var info = new File()
    {
        FileID = Guid.NewGuid(),
        Name = name,
        Description = description,
        FileName = file.FileName,
        BuildID = Guid.Parse(SelectedBuildID),
        MimeType = file.ContentType,
        CreatedUserID = CurrentUser.UserID,
        UpdatedUserID = CurrentUser.UserID,
        Amount = amount,
    };
    var cmmnt = new Comment()
    {
        CommentDate = DateTime.Now,
        CommentText = comment,
        FileID = info.FileID,
        UserID = CurrentUser.UserID
    };
    _variationService.AddVariation(info, file.InputStream);
    _variationService.AddComment(cmmnt);
    return Json("Variation Added Sucessfully", JsonRequestBehavior.AllowGet);
}

【问题讨论】:

    标签: c# .net asp.net-mvc-4 unit-testing


    【解决方案1】:

    HttpPostedFileBase 是一个抽象类,因此不能直接实例化。

    创建一个派生自 HttpPostedFileBase 的类并返回您要查找的值。

        class MyTestPostedFileBase : HttpPostedFileBase
    {
    Stream stream;
    string contentType;
    string fileName;
    
    public MyTestPostedFileBase(Stream stream, string contentType, string fileName)
    {
        this.stream = stream;
        this.contentType = contentType;
        this.fileName = fileName;
    }
    
    public override int ContentLength
    {
        get { return (int)stream.Length; }
    }
    
    public override string ContentType
    {
        get { return contentType; }
    }
    
    public override string FileName
    {
        get { return fileName; }
    }
    
    public override Stream InputStream
    {
        get { return stream; }
    }
    
    public override void SaveAs(string filename)
    {
        throw new NotImplementedException();
    }
    }
    

    【讨论】:

    • 我想赞同这个作为答案(我知道这是一个旧帖子,但仍然 - 我正在搜索)。这对于模拟HttpPostedFileBase 非常有效。在下面添加我的 2 美分 - 我正在寻找一种测试文件长度的方法。
    【解决方案2】:

    我认为 @BenjaminPaul 有最好的答案 - 但想添加这个以防其他人想要测试 MyTestPostedFileBase 对象的内容长度。

    我按照上面概述的方式创建了类,然后传递了一个填充了随机字节的流 - 这允许 `MyTestPostedFileBase.ContentLength 返回我需要的可测试值。

    byte[] byteBuffer = new Byte[10];
    Random rnd = new Random();
    rnd.NextBytes(byteBuffer);
    System.IO.MemoryStream testStream = new System.IO.MemoryStream(byteBuffer);
    

    然后实例化它:

    var TestImageFile = new MyTestPostedFileBase(testStream, "test/content", "test-file.png");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多