From fa9616a5868cbde34575464d1c0561f917163550 Mon Sep 17 00:00:00 2001 From: Christian Klemm Date: Sat, 5 Dec 2020 23:23:27 +0100 Subject: [PATCH] Fix binding validation (#41) * Fixed binding validation --- .github/workflows/build.yaml | 10 +++++-- .github/workflows/release.yaml | 2 +- README.md | 6 ----- .../BlazorClientsideSample.Server.csproj | 3 +-- .../BlazorServersideSample.csproj | 2 +- .../Components/MvvmComponentBaseTests.cs | 27 ++++++------------- src/MvvmBlazor.Tests/MvvmBlazor.Tests.csproj | 12 ++++----- .../Components/MvvmComponentBase.cs | 10 +++---- src/MvvmBlazor/MvvmBlazor.csproj | 6 ++--- 9 files changed, 33 insertions(+), 45 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 56532ff..3af3c80 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -1,4 +1,10 @@ -on: [push, pull_request] +on: + push: + branches: + - master + pull_request: + branches: + - master name: Build @@ -14,7 +20,7 @@ jobs: - name: Setup .NET Core uses: actions/setup-dotnet@master with: - dotnet-version: '3.1.300' + dotnet-version: '5.0.x' - name: Build run: dotnet build --configuration Release src diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c8e1225..b62beb9 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -16,7 +16,7 @@ jobs: - name: Setup .NET Core uses: actions/setup-dotnet@master with: - dotnet-version: '3.1.300' + dotnet-version: '5.0.x' - name: Build run: dotnet build --configuration Release src diff --git a/README.md b/README.md index 8ed4e1e..9e012e7 100644 --- a/README.md +++ b/README.md @@ -168,9 +168,3 @@ This sample tries to incorporate a ports and adapters architectural approach and As an example the weather forecast view model just references the interface of the service that is responsible to gather data. For Blazor serverside, it directly populates it, for Blazor serverside it gathers them from an api. The advantages of such patterns will especially get interesting when Blazor Native and Blazor Embedded will be available. -## Known projects -These projects are know to use `MvvmBlazor`. They can be used as a reference for different implementation scenarios that go beyond the samples. - -- [BlazorPlanningPoker](https://github.com/brandon-james105/BlazorPlanningPoker) - -If you want to see your project here, just create an issue. diff --git a/samples/BlazorClientsideSample.Server/BlazorClientsideSample.Server.csproj b/samples/BlazorClientsideSample.Server/BlazorClientsideSample.Server.csproj index e1bd9f1..46f2cd8 100644 --- a/samples/BlazorClientsideSample.Server/BlazorClientsideSample.Server.csproj +++ b/samples/BlazorClientsideSample.Server/BlazorClientsideSample.Server.csproj @@ -1,8 +1,7 @@  - netcoreapp3.1 - 7.3 + net5.0 diff --git a/samples/BlazorServersideSample/BlazorServersideSample.csproj b/samples/BlazorServersideSample/BlazorServersideSample.csproj index d225cc3..7363bb2 100644 --- a/samples/BlazorServersideSample/BlazorServersideSample.csproj +++ b/samples/BlazorServersideSample/BlazorServersideSample.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net5.0 diff --git a/src/MvvmBlazor.Tests/Components/MvvmComponentBaseTests.cs b/src/MvvmBlazor.Tests/Components/MvvmComponentBaseTests.cs index 16dcc53..1e2c3e9 100644 --- a/src/MvvmBlazor.Tests/Components/MvvmComponentBaseTests.cs +++ b/src/MvvmBlazor.Tests/Components/MvvmComponentBaseTests.cs @@ -112,24 +112,21 @@ public void AddBinding_Throws_WhenBindingMemberIsAField() var wemf = new Mock(); var wem = new Mock(); var bindingFactory = new Mock(); - var binding = new Mock(); serviceProvider.Setup(x => x.GetService(typeof(IWeakEventManagerFactory))).Returns(wemf.Object) .Verifiable(); serviceProvider.Setup(x => x.GetService(typeof(IBindingFactory))).Returns(bindingFactory.Object) .Verifiable(); wemf.Setup(x => x.Create()).Returns(wem.Object).Verifiable(); - bindingFactory.Setup(x => x.Create(It.IsAny(), It.IsAny(), - It.IsAny())).Returns(binding.Object).Verifiable(); var component = new TestComponent(serviceProvider.Object); Should.Throw(() => component.AddBinding(viewModel, x => x._testProperty)); serviceProvider.Verify(); wemf.Verify(); - serviceProvider.VerifyNoOtherCalls(); + wemf.VerifyNoOtherCalls(); + serviceProvider.VerifyNoOtherCalls(); bindingFactory.VerifyNoOtherCalls(); - binding.VerifyNoOtherCalls(); wem.VerifyNoOtherCalls(); } @@ -141,24 +138,21 @@ public void AddBinding_Throws_WhenBindingMemberIsAMethod() var wemf = new Mock(); var wem = new Mock(); var bindingFactory = new Mock(); - var binding = new Mock(); serviceProvider.Setup(x => x.GetService(typeof(IWeakEventManagerFactory))).Returns(wemf.Object) .Verifiable(); serviceProvider.Setup(x => x.GetService(typeof(IBindingFactory))).Returns(bindingFactory.Object) .Verifiable(); wemf.Setup(x => x.Create()).Returns(wem.Object).Verifiable(); - bindingFactory.Setup(x => x.Create(It.IsAny(), It.IsAny(), - It.IsAny())).Returns(binding.Object).Verifiable(); var component = new TestComponent(serviceProvider.Object); Should.Throw(() => component.AddBinding(viewModel, x => x.ShouldRender())); serviceProvider.Verify(); wemf.Verify(); + serviceProvider.VerifyNoOtherCalls(); wemf.VerifyNoOtherCalls(); bindingFactory.VerifyNoOtherCalls(); - binding.VerifyNoOtherCalls(); wem.VerifyNoOtherCalls(); } @@ -170,54 +164,47 @@ public void AddBinding_Throws_WhenPropertyExpressionIsNull() var wemf = new Mock(); var wem = new Mock(); var bindingFactory = new Mock(); - var binding = new Mock(); serviceProvider.Setup(x => x.GetService(typeof(IWeakEventManagerFactory))).Returns(wemf.Object) .Verifiable(); serviceProvider.Setup(x => x.GetService(typeof(IBindingFactory))).Returns(bindingFactory.Object) .Verifiable(); wemf.Setup(x => x.Create()).Returns(wem.Object).Verifiable(); - bindingFactory.Setup(x => x.Create(It.IsAny(), It.IsAny(), - It.IsAny())).Returns(binding.Object).Verifiable(); var component = new TestComponent(serviceProvider.Object); Should.Throw(() => component.AddBinding(viewModel, null)); serviceProvider.Verify(); wemf.Verify(); + serviceProvider.VerifyNoOtherCalls(); wemf.VerifyNoOtherCalls(); bindingFactory.VerifyNoOtherCalls(); - binding.VerifyNoOtherCalls(); wem.VerifyNoOtherCalls(); } [Fact] public void AddBinding_Throws_WhenViewModelIsNull() { - var viewModel = new TestViewModel(); var serviceProvider = new Mock(); var wemf = new Mock(); var wem = new Mock(); var bindingFactory = new Mock(); - var binding = new Mock(); serviceProvider.Setup(x => x.GetService(typeof(IWeakEventManagerFactory))).Returns(wemf.Object) .Verifiable(); serviceProvider.Setup(x => x.GetService(typeof(IBindingFactory))).Returns(bindingFactory.Object) .Verifiable(); wemf.Setup(x => x.Create()).Returns(wem.Object).Verifiable(); - bindingFactory.Setup(x => x.Create(It.IsAny(), It.IsAny(), - It.IsAny())).Returns(binding.Object).Verifiable(); var component = new TestComponent(serviceProvider.Object); Should.Throw(() => - component.AddBinding(null, x => x.TestProperty)); + component.AddBinding(null!, x => x.TestProperty)); serviceProvider.Verify(); wemf.Verify(); + serviceProvider.VerifyNoOtherCalls(); wemf.VerifyNoOtherCalls(); bindingFactory.VerifyNoOtherCalls(); - binding.VerifyNoOtherCalls(); wem.VerifyNoOtherCalls(); } @@ -264,6 +251,8 @@ public void Binding_BindingValueChanged_GetsInvoked() It.IsAny())).Returns(binding.Object).Verifiable(); binding.Setup(x => x.GetValue()).Returns("Test").Verifiable(); binding.Setup(x => x.Initialize()).Verifiable(); + binding.SetupAdd(m => m.BindingValueChanged += It.IsAny()).Verifiable(); + var bindingChangedInvoked = false; var component = new TestComponent(serviceProvider.Object) diff --git a/src/MvvmBlazor.Tests/MvvmBlazor.Tests.csproj b/src/MvvmBlazor.Tests/MvvmBlazor.Tests.csproj index a96ff87..14c7ce9 100644 --- a/src/MvvmBlazor.Tests/MvvmBlazor.Tests.csproj +++ b/src/MvvmBlazor.Tests/MvvmBlazor.Tests.csproj @@ -1,16 +1,16 @@ - + - netcoreapp3.0 + net5.0 false - - - + + + - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MvvmBlazor/Components/MvvmComponentBase.cs b/src/MvvmBlazor/Components/MvvmComponentBase.cs index b7a516f..ec76a04 100644 --- a/src/MvvmBlazor/Components/MvvmComponentBase.cs +++ b/src/MvvmBlazor/Components/MvvmComponentBase.cs @@ -34,9 +34,9 @@ protected MvvmComponentBase() private void InitializeDependencies() { - _weakEventManagerFactory ??= ServiceProvider.GetRequiredService(); - _bindingFactory ??= ServiceProvider.GetRequiredService(); - _weakEventManager ??= _weakEventManagerFactory.Create(); + _weakEventManagerFactory = ServiceProvider.GetRequiredService(); + _bindingFactory = ServiceProvider.GetRequiredService(); + _weakEventManager = _weakEventManagerFactory.Create(); } protected internal TValue Bind(TViewModel viewModel, @@ -87,8 +87,8 @@ protected static PropertyInfo ValidateAndResolveBindingContext - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all - runtime; build; native; contentfiles; analyzers; buildtransitive + runtime; build; native; contentfiles; analyzers