繁体   English   中英

如何测试 ASP.NET MVC controller 并查看

[英]How to test an ASP.NET MVC controller and view

我有一个用 VS2019 编写的简单 ASP.NET MVC 应用程序。 我必须为此 controller 运行单元测试,但从一开始我就没有收到错误。 早些时候,我对示例HomeController进行了测试,一切顺利,现在当我想重复它时,楼梯开始了。

我的 controller:

namespace WebApplication.Controllers
{
    public class BlobController : Controller
    {
        private readonly IBlobStorageRepository repo;

        public BlobController(IBlobStorageRepository _repo)
        {
            this.repo = _repo;
        }
        
        public ActionResult Index()
        {
            var blobVM = repo.GetBlobs();
            return View(blobVM);
        }

        public JsonResult RemoveBlob(string file, string extension)
        {
            bool isDeleted = repo.DeleteBlob(file, extension);
            return Json(isDeleted, JsonRequestBehavior.AllowGet);
        }

        public async Task<ActionResult> DownloadBlob(string file, string extension)
        {
            bool isDownloaded = await repo.DownloadBlobAsync(file, extension);
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult UploadBlob()
        {
            return View();
        }

        [HttpPost]
        public ActionResult UploadBlob(HttpPostedFileBase uploadFileName)
        {
            bool isUploaded = repo.UploadBlob(uploadFileName);

            if(isUploaded == true)
            {
                return RedirectToAction("Index");
            }

            return View();
        }
    }
}

测试一种方法:

[TestClass]
public class BlobControllerTests
{
        [TestMethod]
        public void Index()
        {
            //Arrnage
           BlobController controller = new BlobController(); 
           //Act
           ViewResult result = controller.Index() as ViewResult;
           //Assert
           Assert.IsNotNull(result);
        }
}

错误:

没有为所需的形式参数“_repo”提供参数

有谁可以帮助我测试所有 controller 吗?

我也想测试一下视图,可以吗? 如果是这样,请寻求具体提示。

看法:

@model IEnumerable<WebApplication2.Models.BlobViewModel>
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}



<div class="container">
    @Html.ActionLink("Upload to Azure Blob", "UploadBlob", new { controller = "Blob" }, new {@class = "btn btn-link"})

    <div class="table table-striped table-responsive">

     <table id="tablex">
         <thead>
             <tr>
                 <th>Container</th>
                 <th>Actual FileName</th>
                 <!--<th>Uri</th>-->
             </tr>
         </thead>

         <tbody>
             @if(Model != null)
             {
                 foreach(var item in Model)
                {
             <tr id="row_@item.PrimaryUri">
                 <td>@item.BlobContainerName</td>
                 <td>@item.ActualFileName</td>
                 <!--
    <td>
        <a href=@item.PrimaryUri>@item.PrimaryUri </a>
    </td>-->
                 <td>@Html.ActionLink("Download", "DownloadBlob", new { controller = "Blob", file = @item.FileNameWithoutExt, extension = @item.FileNameExtensionOnly }, new { @class = "btn btn-link" }) </td>
                 <td>
                     <input type="submit" href="#" class="btn btn-link" id="btndel" value="Remove" data-id="@item.ActualFileName" />
                 </td>
             </tr>
                    
                }
             }
         </tbody>
     </table>    
    </div>
</div>

@section scripts{

    <script type="text/javascript">
        debugger
        $(document).ready(function () {
            $('table tbody tr td input[type="submit"]').click(function () {

                var fileName = $(this).attr("data-id")
                var ext = fileName.split('.').pop();
                var file = fileName.substr(0, fileName.lastIndexOf('.'));
                var tr = $(this).closest('tr');

                var msgx = confirm("are u sure to delete?");
                if (msgx) {
                    $.ajax({
                        type: "post",
                        url: '@Url.Action("RemoveBlob", "Blob")',
                        data: { file: file, extension: ext },
                        success: function (response) {
                            if (response == true) {
                                tr.remove();
                            }
                        }
                    });
                }
            });
        });
    </script>
    }

尝试这个

[TestClass]
public class BlobControllerTests
{
        private Mock<IBlobStorageRepository> repo { get; set; } = new Mock<IBlobStorageRepository>();

        [TestMethod]
        public void Index()
        {
            // Arrange
            BlobController controller = new BlobController(_repo: repo.Object); 
            // Act
            ViewResult result = controller.Index() as ViewResult;
            // Assert
            Assert.IsNotNull(result);
        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM