public void GenerateLabel_LabelContentOnContextAndAspFor_ReturnsContentFromContext() { // Arrange var formGroupContext = new TestFormGroupContext(); formGroupContext.SetLabel(isPageHeading: false, attributes: null, content: new HtmlString("Context label")); var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), new Model()) .GetExplorerForProperty(nameof(Model.SimpleProperty)); var viewContext = new ViewContext(); var modelExpression = nameof(Model.SimpleProperty); var modelHelper = new Mock <IModelHelper>(); modelHelper.Setup(mock => mock.GetDisplayName(viewContext, modelExplorer, modelExpression)).Returns("ModelMetadata label"); var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object) { AspFor = new ModelExpression(modelExpression, modelExplorer), Id = "test", ViewContext = viewContext }; // Act var label = tagHelper.GenerateLabel(formGroupContext); // Assert Assert.NotNull(label); Assert.Contains("Context label", label.RenderToString()); }
public void GenerateLabel_NoLabelContentOnContextOrAspFor_ThrowsInvalidOperationException() { // Arrange var formGroupContext = new TestFormGroupContext(); var modelHelper = new Mock <IModelHelper>(); var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object) { Id = "test" }; // Act var ex = Record.Exception(() => tagHelper.GenerateLabel(formGroupContext)); // Assert Assert.IsType <InvalidOperationException>(ex); Assert.Equal("Label content must be specified when the 'asp-for' attribute is not specified.", ex.Message); }
public void GenerateLabel_LabelContentOnContext_ReturnsContentFromContext() { // Arrange var formGroupContext = new TestFormGroupContext(); formGroupContext.SetLabel(isPageHeading: false, attributes: null, content: new HtmlString("Context label")); var modelHelper = new Mock <IModelHelper>(); var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object) { Id = "test" }; // Act var label = tagHelper.GenerateLabel(formGroupContext); // Assert Assert.NotNull(label); Assert.Contains("Context label", label.RenderToString()); }