2
ViewBag
값을 마지막으로 가질 수있는 인덱스 페이지가 있습니다. 나는 (ProductManagementController
)Mvc 5 단위 테스트 - 컨트롤러의 viewbag 값을 설정하십시오.
색인 작업을 테스트중인 내 시스템을 호출하기 전에이 ViewBag
값을 설정할 수 있어요 그래서 나는 설정에 내 컨트롤러를 원하는
[HttpPost]
public async Task<ActionResult> Index(ProductManagementVm postedVm)
{
// Reset pagination if new search
if (postedVm.BookSearch != ViewBag.lastSearch)
{
postedVm.Page = 1;
}
var httpResponseMessage = await_httpService.GetAsync(_urlConfigurations.GetProductList);
var vm = _productFactory.BuildProductManagementVm(
await Task.Run(() => httpResponseMessage.Content.ReadAsStringAsync()), postedVm);
vm.BookSearch = postedVm.BookSearch;
if (string.IsNullOrEmpty(postedVm.BookSearch))
{
postedVm.BookSearch = string.Empty;
}
ViewBag.lastSearch = postedVm.BookSearch;
return View(vm);
}
설정 클래스
using System.ComponentModel.DataAnnotations;
using System.Net.Http;
using System.Web.Mvc;
using ICEBookshop.MVC.App.Controllers;
using ICEBookshop.MVC.App.Interfaces;
using ICEBookshop.MVC.App.Interfaces.Factories;
using ICEBookshop.MVC.App.Models;
using ICEBookshop.MVC.App.Models.ViewModels;
using Moq;
using SpecsFor;
namespace ICEBookshop.MVC.App.Tests.Controllers.ProductManagement
{
public class BaseGiven : SpecsFor<ProductManagementController>
{
protected Mock<IHttpService> HttpServiceMock = new Mock<IHttpService>();
protected Mock<IProductFactory> ProductFactoryMock = new Mock<IProductFactory>();
protected Mock<IUrlConfigurations> UrlConfigurationsMock = new Mock<IUrlConfigurations>();
protected Mock<IJsonValidator> JsonValidatorMock = new Mock<IJsonValidator>();
protected ProductManagementController ProductManagementController;
protected HttpResponseMessage HttpResponseMessage;
protected string JsonContent;
protected bool IsModelStateValid;
protected ActionResult ActionResult;
protected RedirectToRouteResult RedirectToRouteResult;
protected ViewResult ViewResult;
protected ProductManagementVm ProductManagementVm;
protected ProductViewModel ProductViewModel;
protected void BaseGivenSetup()
{
ProductManagementController = new ProductManagementController(HttpServiceMock.Object,
ProductFactoryMock.Object, UrlConfigurationsMock.Object, JsonValidatorMock.Object);
SUT = ProductManagementController;
}
}
}
ProductManagementController.ViewBag.SomeName = "some string"
을 설정하고 싶습니다. 컨트롤러에 들어가면 그 시나리오를 테스트하지만 순간에는입니다..
테스트하기 전에 컨트롤러의 값을 ViewBag
으로 설정하는 방법을 아는 사람이 있습니까?
단위 테스트
public class WhenServiceReturnProductsAndViewBagHasSearchString : GivenGoingToIndexActionInProductsManagement
{
protected override void When()
{
HttpResponseMessage = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("some string content from the service")
};
HttpServiceMock.Setup(expr => expr.GetAsync(It.IsAny<string>())).ReturnsAsync(HttpResponseMessage);
ProductFactoryMock.Setup(
expr => expr.BuildProductManagementVm(It.IsAny<string>(), It.IsAny<ProductManagementVm>()))
.Returns(new ProductManagementVm());
// This doesn't work :(
SUT.ViewBag.LastSearch = "Hey I'm a search string :D";
BaseGivenSetup();
ActionResult = SUT.Index(new ProductManagementVm()).Result;
ViewResult = (ViewResult)ActionResult;
ProductManagementVm = (ProductManagementVm)ViewResult.Model;
}
[Test]
public void ThenActionResultIsNotNull()
{
Assert.IsNotNull(ActionResult);
}
[Test]
public void ThenAViewResultIsNotNull()
{
Assert.IsNotNull(ViewResult);
}
[Test]
public void ThenProductManagementVmIsNotNull()
{
Assert.IsNotNull(ProductManagementVm);
}
}
해결 되었습니까? – Nkosi