From 1ac8ff104e0153abdc5c74e6f6645bf5af3c1960 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Sun, 29 Sep 2019 22:06:09 +0100 Subject: [PATCH] Add specific unit test in ParameterViewTest --- .../Components/test/ParameterViewTest.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Components/Components/test/ParameterViewTest.cs b/src/Components/Components/test/ParameterViewTest.cs index 8aad6c9140..5193e8fe29 100644 --- a/src/Components/Components/test/ParameterViewTest.cs +++ b/src/Components/Components/test/ParameterViewTest.cs @@ -322,6 +322,32 @@ namespace Microsoft.AspNetCore.Components Assert.Same(myEntryValue, result); } + [Fact] + public void CannotReadAfterLifetimeExpiry() + { + // Arrange + var builder = new RenderBatchBuilder(); + var lifetime = new ParameterViewLifetime(builder); + var frames = new[] + { + RenderTreeFrame.ChildComponent(0, typeof(FakeComponent)).WithComponentSubtreeLength(1) + }; + var parameterView = new ParameterView(lifetime, frames, 0); + + // Act + builder.InvalidateParameterViews(); + + // Assert + Assert.Throws(() => parameterView.GetEnumerator()); + Assert.Throws(() => parameterView.GetValueOrDefault("anything")); + Assert.Throws(() => parameterView.SetParameterProperties(new object())); + Assert.Throws(() => parameterView.ToDictionary()); + var ex = Assert.Throws(() => parameterView.TryGetValue("anything", out _)); + + // It's enough to assert about one of the messages + Assert.Equal($"The {nameof(ParameterView)} instance can no longer be read because it has expired. {nameof(ParameterView)} can only be read synchronously and must not be stored for later use.", ex.Message); + } + private Action AssertParameter(string expectedName, object expectedValue, bool expectedIsCascading) { return parameter =>