UnitTest无法找到端点

问题描述 投票:0回答:1

使用xUnit 2.4.1测试Api始终无法找到Controller

当我创建WebApplicationFactory并将Startup文件作为参数传递时,来自WebApplicationFactory.CreatVlient()的HTTP Client始终为Get请求返回404。

测试使用MVC的.Net Core Api。

CommonContext是一个设置连接的内部类。

Configfile读得正确 Connectionstring到DB是正确的 端点未正确调用,因此永远不会命中控制器。

继承WebApplicationFactory的类

    public class WebApiTestFactory<TStartup>
        : WebApplicationFactory<TStartup> where TStartup: class
    {

        protected override IWebHostBuilder CreateWebHostBuilder()
        {
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddEnvironmentVariables();

            var configValues = new Dictionary<string, string>
            {
                {"RunStatus", "Test"},
            };
            builder.AddInMemoryCollection(configValues);

            return WebHost.CreateDefaultBuilder()
                .UseConfiguration(builder.Build())
                .UseUrls("http://localhost:53976/")
                .UseSetting("applicationUrl", "http://localhost:53976/")
                .UseStartup<Else.WebApi.Core.Startup>();
        }
    }

单元测试

  public class TestControllerTest : IClassFixture<WebApiTestFactory<Startup>>
    {
        private readonly WebApiTestFactory<Startup> _factory;

        public TestControllerTest(WebApiTestFactory<Startup> factory)
        {

            _factory = factory;

        }

        [Theory]
        [InlineData("api/Test/GetExample")]
        public async Task Create(string url)
        {

            // Arrange
            var clientOptions = new WebApplicationFactoryClientOptions();
            clientOptions.BaseAddress = new Uri("http://localhost:53976");
            var client = _factory.CreateClient(clientOptions);

            // Act
            var response = await client.GetAsync(url);

            // Assert
            response.EnsureSuccessStatusCode(); // Status Code 200-299
            Assert.Equal("text/html; charset=utf-8",
                response.Content.Headers.ContentType.ToString());
        }
    }

控制器正在进行项目测试

    [ApiController]
    [Route("api/Test")]
    public class TestController : Controller
    {

        [HttpGet("GetExample")]
        public ActionResult GetExample()
        {
            return Ok();
        }

    }

启动

   public class Startup
    {
        public Startup(IConfiguration configuration, IHostingEnvironment env)
        {

            HostingEnvironment = env;

            Configuration = configuration;

            EwBootstrapper.BootstrapElsewareServices();
        }

        public IConfiguration Configuration { get; }
        public IHostingEnvironment HostingEnvironment { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {



            if (Configuration["RunStatus"] != "Test")
            {
                services.AddTransient<AuthenticationTokens>();
                services.AddTransient<IPasswordValidator, PasswordValidator>();
                services.AddTransient<IUserRepository, UserRepository>();

                services.AddMvc();

                services.AddScoped(_ =>
           new CommonContext(Configuration.GetConnectionString("DbConnection")));

                services.AddSwaggerDocumentation();

                services
                    .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) // Configure authentication (JWT bearer)
                    .AddJwtBearer(jwtOpt => // Configure JWT bearer
                    {
                        jwtOpt.TokenValidationParameters = AuthenticationTokens.GetValidationParameters();
                    });
            }
            else
            {

                //services.AddMvcCore().AddJsonFormatters();
                services.AddScoped(_ =>
           new CommonContext(Configuration.GetConnectionString("DbTestConnection")));

            }
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {

            if (Configuration["RunStatus"] != "Test")
            {
                app.UseSwaggerDocumentation();

                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "API");
                });
                app.UseMiddleware<ApiLoggerMiddleware>();
                app.UseMvc(builder => builder.MapRoute("Default", "api/{controller}/{action=Get}/{id?}")); // No default values for controller or action

                app.UseDefaultFiles(); // Enable default documents ( "/" => "/index.html")
                app.UseStaticFiles(); // Static files under wwwroot
                app.UseAuthentication();


            }

            if (HostingEnvironment.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

            }
        }
    }
c# unit-testing xunit.net
1个回答
1
投票

根据控制器中的属性路由,action方法有url api/Test/GetExample[HttpGet("GetExample")],但在你的测试中你正在测试CreateExample

[InlineData("api/Test/CreateExample")]

所以我想,你的测试在返回404时是正确的。该路由根本不会解析为任何现有的操作方法。我建议你把你的理论改成[InlineData("api/Test/GetExample")]

© www.soinside.com 2019 - 2024. All rights reserved.