How to unit test ViewComponent.Invoke()?

左心房为你撑大大i 提交于 2019-11-26 17:24:05

问题


In ViewComponent object, HttpContext and User are read-only properties.

How to unit test such a component?

I'm using the MSTest Freamwork.

The follow properties are used in my code

  1. Cookie
  2. Session
  3. User(System.Security.Principal)

public ViewViewComponentResult Invoke()
{
    var vm = new SummaryViewModel();
    if (User.Identity is ClaimsIdentity identity && identity.IsAuthenticated)
    {
        vm.IsAuthenticated = true;
        vm.UserName = identity.Claims.FirstOrDefault(c => c.Type == "UserName").Value;
        vm.PhotoUrl = identity.Claims.FirstOrDefault(c => c.Type == "FacePicture").Value;
    }
    return View(vm);
}

[TestMethod]
public void UserSummaryVcTest()
{
    var component = new UserSummaryViewComponent();
    var model = component.Invoke().ViewData.Model as SummaryViewModel;
    Assert.AreEqual("UserName", model.UserName);
} 

回答1:


According to source code the ViewComponent relies on the ViewComponentContext.ViewContext to expose those read only properties, Which in turn accesses the HttpContext. That is your entry point to mock the desired values.

[TestMethod]
public void UserSummaryVcTest() {

    // Arrange
    var expected = "Username value";
    var httpContext = new DefaultHttpContext(); //You can also Mock this
    //...then set user and other required properties on the httpContext as needed

    var viewContext = new ViewContext();
    viewContext.HttpContext = httpContext;
    var viewComponentContext = new ViewComponentContext();
    viewComponentContext.ViewContext = viewContext;

    var viewComponent = new UserSummaryViewComponent();
    viewComponent.ViewComponentContext = viewComponentContext;

    //Act
    var model = viewComponent.Invoke().ViewData.Model as SummaryViewModel;

    //Assert
    Assert.AreEqual(expected, model.UserName);
} 



回答2:


Here is just a samle for async,

    [TestMethod]
        public async System.Threading.Tasks.Task InvokeAsyncNameAsync()
{
# setup mocks
...
var httpContext = new DefaultHttpContext();
            var viewContext = new ViewContext();
            viewContext.HttpContext = httpContext;
            var viewComponentContext = new ViewComponentContext();
            viewComponentContext.ViewContext = viewContext;

            var footerComponent = CreateComponentInstance();
            footerComponent.ViewComponentContext = viewComponentContext;

            ViewViewComponentResult result = await footerComponent.InvokeAsync() as ViewViewComponentResult;
            FooterModel resultModel = (FooterModel)result.ViewData.Model;

....
# do your asserts verifications

            Assert.AreEqual(expectedTest, resultModel.FooterText);
}


来源:https://stackoverflow.com/questions/47174028/how-to-unit-test-viewcomponent-invoke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!