<small id='huZWK'></small><noframes id='huZWK'>

      <legend id='huZWK'><style id='huZWK'><dir id='huZWK'><q id='huZWK'></q></dir></style></legend>
    1. <tfoot id='huZWK'></tfoot>
        <bdo id='huZWK'></bdo><ul id='huZWK'></ul>
      <i id='huZWK'><tr id='huZWK'><dt id='huZWK'><q id='huZWK'><span id='huZWK'><b id='huZWK'><form id='huZWK'><ins id='huZWK'></ins><ul id='huZWK'></ul><sub id='huZWK'></sub></form><legend id='huZWK'></legend><bdo id='huZWK'><pre id='huZWK'><center id='huZWK'></center></pre></bdo></b><th id='huZWK'></th></span></q></dt></tr></i><div id='huZWK'><tfoot id='huZWK'></tfoot><dl id='huZWK'><fieldset id='huZWK'></fieldset></dl></div>
    2. 如何在 ASP.NET Core 中模拟 IFormFile 以进行单元/集成测试?

      How to mock an IFormFile for a unit/integration test in ASP.NET Core?(如何在 ASP.NET Core 中模拟 IFormFile 以进行单元/集成测试?)
        <tbody id='Vb9U3'></tbody>
      <tfoot id='Vb9U3'></tfoot>
          <bdo id='Vb9U3'></bdo><ul id='Vb9U3'></ul>
          <i id='Vb9U3'><tr id='Vb9U3'><dt id='Vb9U3'><q id='Vb9U3'><span id='Vb9U3'><b id='Vb9U3'><form id='Vb9U3'><ins id='Vb9U3'></ins><ul id='Vb9U3'></ul><sub id='Vb9U3'></sub></form><legend id='Vb9U3'></legend><bdo id='Vb9U3'><pre id='Vb9U3'><center id='Vb9U3'></center></pre></bdo></b><th id='Vb9U3'></th></span></q></dt></tr></i><div id='Vb9U3'><tfoot id='Vb9U3'></tfoot><dl id='Vb9U3'><fieldset id='Vb9U3'></fieldset></dl></div>

              <small id='Vb9U3'></small><noframes id='Vb9U3'>

                <legend id='Vb9U3'><style id='Vb9U3'><dir id='Vb9U3'><q id='Vb9U3'></q></dir></style></legend>
              • 本文介绍了如何在 ASP.NET Core 中模拟 IFormFile 以进行单元/集成测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想为在 ASP.NET Core 中上传文件编写测试,但似乎找不到模拟/实例化从 IFormFile 派生的对象的好方法.

                I want to write tests for uploading of files in ASP.NET Core but can't seem to find a nice way to mock/instantiate an object derived from IFormFile.

                关于如何做到这一点的任何建议?

                Any suggestions on how to do this?

                推荐答案

                假设你有一个像这样的控制器..

                Assuming you have a Controller like..

                public class MyController : Controller {
                    public Task<IActionResult> UploadSingle(IFormFile file) {...}
                }
                

                ...使用被测方法访问 IFormFile.OpenReadStream().

                ...where the IFormFile.OpenReadStream() is accessed with the method under test.

                您可以使用 Moq 模拟框架创建测试来模拟流数据.

                You can create a test using Moq mocking framework to simulate the stream data.

                [TestClass]
                public class IFormFileUnitTests {
                    [TestMethod]
                    public async Task Should_Upload_Single_File() {
                        //Arrange
                        var fileMock = new Mock<IFormFile>();
                        //Setup mock file using a memory stream
                        var content = "Hello World from a Fake File";
                        var fileName = "test.pdf";
                        var ms = new MemoryStream();
                        var writer = new StreamWriter(ms);
                        writer.Write(content);
                        writer.Flush();
                        ms.Position = 0;
                        fileMock.Setup(_ => _.OpenReadStream()).Returns(ms);
                        fileMock.Setup(_ => _.FileName).Returns(fileName);
                        fileMock.Setup(_ => _.Length).Returns(ms.Length);
                
                        var sut = new MyController();
                        var file = fileMock.Object;
                
                        //Act
                        var result = await sut.UploadSingle(file);
                
                        //Assert
                        Assert.IsInstanceOfType(result, typeof(IActionResult));
                    }
                }
                

                或者,从 ASP.NET Core 3.0 开始,使用 FormFile 类 现在是 IFormFile 的默认实现.

                Or, as of ASP.NET Core 3.0, use an instance of the FormFile Class which is now the default implementation of IFormFile.

                这是使用 FormFile 类进行上述相同测试的示例

                Here is an example of the same test above using FormFile class

                [TestClass]
                public class IFormFileUnitTests {
                    [TestMethod]
                    public async Task Should_Upload_Single_File() {
                        //Arrange
                       
                        //Setup mock file using a memory stream
                        var content = "Hello World from a Fake File";
                        var fileName = "test.pdf";
                        var stream = new MemoryStream();
                        var writer = new StreamWriter(stream);
                        writer.Write(content);
                        writer.Flush();
                        stream.Position = 0;
                
                        //create FormFile with desired data
                        IFormFile file = new FormFile(stream, 0, stream.Length, "id_from_form", fileName);
                        
                        MyController sut = new MyController();
                        
                        //Act
                        var result = await sut.UploadSingle(file);
                
                        //Assert
                        Assert.IsInstanceOfType(result, typeof(IActionResult));
                    }
                }
                

                这篇关于如何在 ASP.NET Core 中模拟 IFormFile 以进行单元/集成测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to keep the Text of a Read only TextBox after PostBack()?(PostBack()之后如何保留只读文本框的文本?)
                Winforms Textbox - Using Ctrl-Backspace to Delete Whole Word(Winforms 文本框 - 使用 Ctrl-Backspace 删除整个单词)
                C# - Add button click events using code(C# - 使用代码添加按钮单击事件)
                Multi-color TextBox C#(多色文本框 C#)
                How can i set the caret position to a specific index in passwordbox in WPF(如何将插入符号位置设置为 WPF 密码框中的特定索引)
                C# Numeric Only TextBox Control(C# 纯数字文本框控件)

                • <bdo id='byKLT'></bdo><ul id='byKLT'></ul>
                  <tfoot id='byKLT'></tfoot>
                  <legend id='byKLT'><style id='byKLT'><dir id='byKLT'><q id='byKLT'></q></dir></style></legend>
                        <tbody id='byKLT'></tbody>

                        <small id='byKLT'></small><noframes id='byKLT'>

                          <i id='byKLT'><tr id='byKLT'><dt id='byKLT'><q id='byKLT'><span id='byKLT'><b id='byKLT'><form id='byKLT'><ins id='byKLT'></ins><ul id='byKLT'></ul><sub id='byKLT'></sub></form><legend id='byKLT'></legend><bdo id='byKLT'><pre id='byKLT'><center id='byKLT'></center></pre></bdo></b><th id='byKLT'></th></span></q></dt></tr></i><div id='byKLT'><tfoot id='byKLT'></tfoot><dl id='byKLT'><fieldset id='byKLT'></fieldset></dl></div>