2017-04-10 3 views
2

Ich habe eine Indexseite, die einen ViewBag Wert der letzten Suche haben könnte. Ich möchte, dass meine Controller-Setup so ich in der Lage bin dieses ViewBag Wert zu setzen, bevor mein testende System (ProductManagementController)Mvc 5 Unit Tests - Set Viewbag Wert des Controllers

Index Aktion Aufruf

[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); 
} 

Setup-Klasse

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; 
     } 
    } 
} 

Ich möchte ProductManagementController.ViewBag.SomeName = "some string" setzen, also wenn ich in den Controller eintrete, teste ich dieses Szenario, aber im Moment ist es null.

Kann jemand einen ViewBag Wert eines Controllers vor dem Testen setzen?

Unit-Test

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); 
    } 
} 
+0

Wurde das gelöst? – Nkosi

Antwort

1

ViewBag erhält seine Daten von der ViewData Eigenschaft

public dynamic ViewBag 
{ 
    get 
    { 
     if (_dynamicViewDataDictionary == null) 
     { 
      _dynamicViewDataDictionary = new DynamicViewDataDictionary(() => ViewData); 
     } 
     return _dynamicViewDataDictionary; 
    } 
} 

So müssen Sie den Wert, den Sie dort Zugang zu haben, um es in der ViewBag

wollen füllen

Hier ist ein POC

[TestClass] 
public class ViewBagTests { 
    [TestMethod] 
    public void ViewBag_ShouldBe_PrePopulated() { 
     //Arrange 
     var SUT = new TargetController(); 

     var expected = "Hey I'm the old search string :D"; 

     SUT.ViewData["LastSearch"] = expected; 

     //Act 
     var actual = SUT.Index() as ViewResult; 

     //Assert 
     Assert.AreEqual(expected, actual.Model); 
    } 

    class TargetController : Controller { 
     public ActionResult Index() { 
      var previous = ViewBag.LastSearch; 
      return View((object)previous); 
     } 
    } 

}