/// <summary> /// Tests <paramref name="model"/> type and (optionally) calls validation callback function. /// </summary> /// <param name="model">The model to be tested.</param> public void Test(object model) { if (model == null || !TypeTester.IsOfType(model, typeof(TModel))) { string actualTypeName = (model == null) ? "null" : model.GetType().FullName; throw new ActionTestException( $"Expected model type: {typeof(TModel).FullName}. Actual: {actualTypeName}."); } _validate?.Invoke((TModel)model); }
private TResultType AssertViewComponentResultType <TResultType>(IViewComponentResult viewComponentResult) where TResultType : IViewComponentResult { Type expectedType = typeof(TResultType); if (viewComponentResult == null || !TypeTester.IsOfType(viewComponentResult, expectedType)) { string actualTypeName = (viewComponentResult == null) ? "null" : viewComponentResult.GetType().FullName; throw new ActionTestException( $"Expected IViewComponentResult type {expectedType.FullName}. Actual: {actualTypeName}."); } return((TResultType)viewComponentResult); }
private TActionResultType AssertActionResultType <TActionResultType>(IActionResult actionResult) where TActionResultType : IActionResult { Type expectedType = typeof(TActionResultType); if (actionResult == null || !TypeTester.IsOfType(actionResult, expectedType)) { string actualTypeName = (actionResult == null) ? "null" : actionResult.GetType().FullName; throw new ActionTestException( $"Expected IActionResult type {expectedType.FullName}. Actual: {actualTypeName}."); } return((TActionResultType)actionResult); }
private TViewComponent AssertIsViewComponent(TViewComponent viewComponent) { Type thisType = typeof(TViewComponent); if (TypeTester.IsOfType(viewComponent, typeof(ViewComponent)) || thisType.Name.EndsWith("ViewComponent") || thisType.GetCustomAttributes(typeof(ViewComponentAttribute), inherit: true).Length > 0) { return(viewComponent); } throw new ActionTestException( "A ViewComponent must inherit from ViewComponent" + ", have a name ending with \"ViewComponent\", or have a [ViewComponent] attribute." + $" {thisType.FullName} is not a valid ViewComponent type."); }