Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RouteBuilderTest.cs « UnitTests « test « Routing « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c671247a3184878f9336bbd7f346602f77168c29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;

namespace Microsoft.AspNetCore.Routing;

public class RouteBuilderTest
{
    [Fact]
    public void Ctor_SetsPropertyValues()
    {
        // Arrange
        var services = new ServiceCollection();
        services.AddSingleton(typeof(RoutingMarkerService));
        var applicationServices = services.BuildServiceProvider();
        var applicationBuilderMock = new Mock<IApplicationBuilder>();
        applicationBuilderMock.Setup(a => a.ApplicationServices).Returns(applicationServices);
        var applicationBuilder = applicationBuilderMock.Object;
        var defaultHandler = Mock.Of<IRouter>();

        // Act
        var builder = new RouteBuilder(applicationBuilder, defaultHandler);

        // Assert
        Assert.Same(applicationBuilder, builder.ApplicationBuilder);
        Assert.Same(defaultHandler, builder.DefaultHandler);
        Assert.Same(applicationServices, builder.ServiceProvider);
    }

    [Fact]
    public void Ctor_ThrowsInvalidOperationException_IfRoutingMarkerServiceIsNotRegistered()
    {
        // Arrange
        var applicationBuilderMock = new Mock<IApplicationBuilder>();
        applicationBuilderMock
            .Setup(s => s.ApplicationServices)
            .Returns(Mock.Of<IServiceProvider>());

        // Act & Assert
        var exception = Assert.Throws<InvalidOperationException>(() => new RouteBuilder(applicationBuilderMock.Object));

        Assert.Equal(
            "Unable to find the required services. Please add all the required services by calling " +
            "'IServiceCollection.AddRouting' inside the call to 'ConfigureServices(...)'" +
            " in the application startup code.",
            exception.Message);
    }
}