Unit Testing Async View Component In ASP.Net Core NUnit Tests

40 views Asked by At

I'm Trying to write tests for this function

        public async Task<IViewComponentResult> InvokeAsync()
        {
            var model = await _modelBinder.Bind<Breadcrumbs>(this.ViewContext);
            model.BreadcrumbsList.Reverse();
            return View("_Breadcrumbs", model);
        }

However I am having trouble getting the returned model in the test. I start with something like this but can't figure out the process to get the model out of the returned IViewComponentResult. I've tried casting to ViewResult but get an error.

        [Test]
        public async Task BreadcrumbsViewComponent_GivenListOfBreadcrumbs_ReverseBreadcrumbs()
        {
            var breadcrumbsModel = new Breadcrumbs
            {
                BreadcrumbsList = new List<ItemLinkField<Breadcrumb>>
                {
                    new ItemLinkField<Breadcrumb> { Url = "/Page1" },
                    new ItemLinkField<Breadcrumb> { Url = "/ResourceCenter" },
                    new ItemLinkField<Breadcrumb> { Url = "/" }
                }
            };
            _moqViewModelBinder.Setup(vmb => vmb.Bind<Breadcrumbs>(It.IsAny<ViewContext>())).ReturnsAsync(breadcrumbsModel);



            var breadcrumbsViewComponent = GetSut();

            var result = await breadcrumbsViewComponent.InvokeAsync();
        }

Tried to get the model using casting to no luck.

0

There are 0 answers