0

I have a simple ASP.NET MVC app wrote in VS2019. I have to run unit tests for this controller but I don't get an error from the very beginning. Earlier, I ran tests for the example HomeController and everything went well, now when I want to repeat it, the stairs are starting.

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

Test to one method:

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

Error:

No argument was given to the required formal parameter "_repo"

Does anyone could help me with testing that all controller?

I would also like to test the view, is it possible? If so asking for specific tips.

View:

@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="[email protected]">
                 <td>@item.BlobContainerName</td>
                 <td>@item.ActualFileName</td>
                 <!--
    <td>
        <a [email protected]>@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>
    }
3
  • 1
    For testing your controller - BlobController controller = new BlobController(); line needs to supply an IBlobStorageRepository object as a parameter. You probably want to mock it. Commented Jun 6, 2021 at 11:46
  • I treid BlobController controller = new BlobController(_repo: IBlobStorageRepository); and thats wrong type Commented Jun 6, 2021 at 11:53
  • 2
    You need to learn about mocking the dependency for unit testing Commented Jun 6, 2021 at 12:52

1 Answer 1

1

Try this

[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);
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It, works! Could you help me with test view?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.