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>
}
BlobController controller = new BlobController();line needs to supply anIBlobStorageRepositoryobject as a parameter. You probably want to mock it.