覆盖多个子类中的处理方法

问题描述

我目前正在使用 Xunit 在我的 .NET Core 项目中编写一些集成测试。作为其中的一部分,我在自定义类中运行测试之前设置数据和其他信息。

using Microsoft.AspNetCore.TestHost;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Xunit;


namespace XUnitTestProject1
    {
        public class BaseClassFixture<TStartup> : IAsyncLifetime where TStartup : class
        {
            private readonly TestServer _server;

            public HttpClient Client { get; }

            public BaseClassFixture()
            {
                Client = new HttpClient();

                Client.BaseAddress = new Uri("https://apple.com");

                Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            }


            protected virtual void dispose(bool disposing)
            {
                if (disposing)
                {
                    _server?.dispose();
                    this.Client?.dispose();
                }
            }

            public Task disposeAsync()
            {
                return Task.CompletedTask;
            }

            public Task InitializeAsync()
            {
                throw new NotImplementedException();
            }
        }
    }

我的第一个儿童班:

using Microsoft.AspNetCore.TestHost;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace XUnitTestProject1
{
    public class GoogleTestClassFixture<TStartUp> : BaseClassFixture<TStartUp>,IAsyncLifetime where TStartUp : class
    {
        private readonly TestServer _server;

        public HttpClient GoogleClient { get; }

        public GoogleTestClassFixture()
        {
            GoogleClient = new HttpClient();

            GoogleClient.BaseAddress = new Uri("https://google.com");

            GoogleClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }


        public void dispose()
        {
            _server?.dispose();
            GoogleClient?.dispose();
            base.dispose(true);

        }
        public Task disposeAsync()
        {
            throw new NotImplementedException();
        }

        public Task InitializeAsync()
        {
            throw new NotImplementedException();
        }

    }

    }

我的第二个孩子班:

 using Microsoft.AspNetCore.TestHost;
    using System;
    using System.Collections.Generic;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text;
    using System.Threading.Tasks;
    using Xunit;
    
    namespace XUnitTestProject1
    {
        public class FaceBookTestClassFixture<TStartUp> : BaseClassFixture<TStartUp>,IAsyncLifetime where TStartUp : class
        {
            private readonly TestServer _server;
    
            public HttpClient FaceBookClient { get; }
    
            public FaceBookTestClassFixture()
            {
                FaceBookClient = new HttpClient();
    
                FaceBookClient.BaseAddress = new Uri("https://facebook.com");
    
                FaceBookClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            }
    
    
            public void dispose()
            {
                _server?.dispose();
                FaceBookClient?.dispose();
                base.dispose(true);
    
            }
            public Task disposeAsync()
            {
                throw new NotImplementedException();
            }
    
            public Task InitializeAsync()
            {
                throw new NotImplementedException();
            }
    
    
        }
    
    }

当我一次在测试资源管理器中运行所有测试时,有时我会收到错误“无法访问处置对象”。我假设,发生这种情况是因为有时与一个测试夹具相关的测试正在运行并且对象已被释放,而另一个测试夹具无法从基类夹具访问已被释放的项目。

我的第一个测试看起来像这样:

       public class GoogleTests
    {
        private readonly HttpClient _googleClient;
        private readonly HttpClient _baseClient;

        public GoogleTests(GoogleTestClassFixture<StartUp> fixture)
        {
            _googleClient = fixture.GoogleClient;
            _baseClient = fixture.Client;
        }

        [Fact]
        public async Task ValidateFirstClient()
        {
            var getDetails = await _googleClient.GetAsync("//someRoute");
            var uri = new Uri("https://someRandom.com");
            var postDetails = await _baseClient.PutAsync(uri,getDetails.Content);
            var response = await postDetails.Content.ReadAsstringAsync();
            dynamic dynamicResponse = JObject.Parse(response);
            ((int)dynamicResponse.myProperty).Should().Be(0);
        }
    }

我的第二堂测试课:

using FluentAssertions;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace XUnitTestProject1
{
    public class FaceBookTests
    {
        private readonly HttpClient _fbClient;
        private readonly HttpClient _baseClient;

        public FaceBookTests(FaceBookTestClassFixture<StartUp> fixture)
        {
            _fbClient = fixture.FaceBookClient;
            _baseClient = fixture.Client;
        }

        [Fact]
        public async Task ValidateFirstClient()
        {
            var getDetails = await _fbClient.GetAsync("//someRoute");
            var uri = new Uri("https://someRandom.com");
            var postDetails = await _baseClient.PutAsync(uri,getDetails.Content);
            var response = await postDetails.Content.ReadAsstringAsync();
            dynamic dynamicResponse = JObject.Parse(response);
            ((int)dynamicResponse.myProperty).Should().Be(0);
        }
    }
}

    enter code here

我该如何解决这个问题,并确保在完成所有测试之前不会释放对象? 注意:我创建了一个简单的 web 应用程序项目只是为了在我的测试项目中获取 startup.cs 类作为参考

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...