Пример #1
0
        public async Task DisposeAsync_CanDisposeAfterCallingRunAsync()
        {
            // Arrange
            var builder = new WebAssemblyHostBuilder(new TestJSUnmarshalledRuntime());

            builder.Services.AddSingleton <DisposableService>();
            var host = builder.Build();

            host.CultureProvider = new TestSatelliteResourcesLoader();

            var disposable = host.Services.GetRequiredService <DisposableService>();

            var cts = new CancellationTokenSource();

            // Act
            await using (host)
            {
                var task = host.RunAsyncCore(cts.Token);

                cts.Cancel();
                await task.TimeoutAfter(TimeSpan.FromSeconds(3));
            }

            // Assert
            Assert.Equal(1, disposable.DisposeCount);
        }
Пример #2
0
        public void Build_AllowsConfiguringServices()
        {
            // Arrange
            var builder = new WebAssemblyHostBuilder(new TestJSUnmarshalledRuntime());

            // This test also verifies that we create a scope.
            builder.Services.AddScoped <StringBuilder>();

            // Act
            var host = builder.Build();

            // Assert
            Assert.NotNull(host.Services.GetRequiredService <StringBuilder>());
        }
Пример #3
0
        public void Build_InDevelopment_ConfiguresWithServiceProviderWithScopeValidation()
        {
            // Arrange
            var builder = new WebAssemblyHostBuilder(new TestJSUnmarshalledRuntime(environment: "Development"));

            builder.Services.AddScoped <StringBuilder>();
            builder.Services.AddSingleton <TestServiceThatTakesStringBuilder>();

            // Act
            var host = builder.Build();

            // Assert
            Assert.NotNull(host.Services.GetRequiredService <StringBuilder>());
            Assert.Throws <InvalidOperationException>(() => host.Services.GetRequiredService <TestServiceThatTakesStringBuilder>());
        }
Пример #4
0
        public void Builder_CreatesNavigationManager()
        {
            // Arrange
            var builder = new WebAssemblyHostBuilder(new TestJSUnmarshalledRuntime(environment: "Development"));

            // Act
            var host = builder.Build();

            // Assert
            var navigationManager = host.Services.GetRequiredService <NavigationManager>();

            Assert.NotNull(navigationManager);
            Assert.Equal("https://www.example.com/", navigationManager.BaseUri);
            Assert.Equal("https://www.example.com/awesome-part-that-will-be-truncated-in-tests/cool", navigationManager.Uri);
        }
Пример #5
0
        public void Build_InProduction_ConfiguresWithServiceProviderWithScopeValidation()
        {
            // Arrange
            var builder = new WebAssemblyHostBuilder(new TestJSUnmarshalledRuntime());

            builder.Services.AddScoped <StringBuilder>();
            builder.Services.AddSingleton <TestServiceThatTakesStringBuilder>();

            // Act
            var host = builder.Build();

            // Assert
            Assert.NotNull(host.Services.GetRequiredService <StringBuilder>());
            Assert.NotNull(host.Services.GetRequiredService <TestServiceThatTakesStringBuilder>());
        }
Пример #6
0
        public void Builder_SupportsConfiguringLogging()
        {
            // Arrange
            var builder  = new WebAssemblyHostBuilder(new TestJSUnmarshalledRuntime());
            var provider = new Mock <ILoggerProvider>();

            // Act
            builder.Logging.AddProvider(provider.Object);
            var host = builder.Build();

            // Assert
            var loggerProvider = host.Services.GetRequiredService <ILoggerProvider>();

            Assert.NotNull(loggerProvider);
            Assert.Equal <ILoggerProvider>(provider.Object, loggerProvider);
        }
Пример #7
0
        public void Build_AllowsConfiguringConfiguration()
        {
            // Arrange
            var builder = new WebAssemblyHostBuilder(new TestJSUnmarshalledRuntime());

            builder.Configuration.AddInMemoryCollection(new[]
            {
                new KeyValuePair <string, string>("key", "value"),
            });

            // Act
            var host = builder.Build();

            // Assert
            Assert.Equal("value", host.Configuration["key"]);
        }
Пример #8
0
        public void Build_AllowsConfiguringContainer()
        {
            // Arrange
            var builder = new WebAssemblyHostBuilder(new TestJSUnmarshalledRuntime());

            builder.Services.AddScoped <StringBuilder>();
            var factory = new MyFakeServiceProviderFactory();

            builder.ConfigureContainer(factory);

            // Act
            var host = builder.Build();

            // Assert
            Assert.True(factory.CreateServiceProviderCalled);
            Assert.NotNull(host.Services.GetRequiredService <StringBuilder>());
        }
Пример #9
0
        public async Task RunAsync_CanExitBasedOnCancellationToken()
        {
            // Arrange
            var builder         = new WebAssemblyHostBuilder(new TestJSUnmarshalledRuntime());
            var host            = builder.Build();
            var cultureProvider = new TestSatelliteResourcesLoader();

            var cts = new CancellationTokenSource();

            // Act
            var task = host.RunAsyncCore(cts.Token, cultureProvider);

            cts.Cancel();
            await task.TimeoutAfter(TimeSpan.FromSeconds(3));

            // Assert (does not throw)
        }
Пример #10
0
        public void Build_AddsConfigurationToServices()
        {
            // Arrange
            var builder = new WebAssemblyHostBuilder(new TestJSUnmarshalledRuntime());

            builder.Configuration.AddInMemoryCollection(new[]
            {
                new KeyValuePair <string, string>("key", "value"),
            });

            // Act
            var host = builder.Build();

            // Assert
            var configuration = host.Services.GetRequiredService <IConfiguration>();

            Assert.Equal("value", configuration["key"]);
        }
Пример #11
0
        public async Task RunAsync_CallingTwiceCausesException()
        {
            // Arrange
            var builder         = new WebAssemblyHostBuilder(new TestJSUnmarshalledRuntime());
            var host            = builder.Build();
            var cultureProvider = new TestSatelliteResourcesLoader();

            var cts  = new CancellationTokenSource();
            var task = host.RunAsyncCore(cts.Token, cultureProvider);

            // Act
            var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => host.RunAsyncCore(cts.Token));

            cts.Cancel();
            await task.TimeoutAfter(TimeSpan.FromSeconds(3));

            // Assert
            Assert.Equal("The host has already started.", ex.Message);
        }
Пример #12
0
        public void Build_AllowsConfiguringContainer_WithDelegate()
        {
            // Arrange
            var builder = new WebAssemblyHostBuilder(new TestWebAssemblyJSRuntimeInvoker());

            builder.Services.AddScoped <StringBuilder>();

            var factory = new MyFakeServiceProviderFactory();

            builder.ConfigureContainer(factory, builder =>
            {
                builder.ServiceCollection.AddScoped <List <string> >();
            });

            // Act
            var host = builder.Build();

            // Assert
            Assert.True(factory.CreateServiceProviderCalled);
            Assert.NotNull(host.Services.GetRequiredService <StringBuilder>());
            Assert.NotNull(host.Services.GetRequiredService <List <string> >());
        }