[ad_1]
こんにちは、
私のアプリはうまくいっています。 しかし、単体テストを作成しようとすると、エンティティ フレームワークがうまく機能しません。
Entity Framework の単体テストを作成しようとしていますが、サーバーからデータを取得/書き込みしようとすると、次のエラーが発生します。
System.ObjectDisposedException : Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'FB_SERVICEContext'.
真犯人は? アドバイスが必要です。
単体テスト:
[TestFixture()] public class FileControllerTests { private DependencyResolver _dependencyResolver; public FileController _controller { get; set; } public FB_SERVICEContext _context { get; set; } public FileImageDataService _service { get; set; } public ParamAppService _param{ get; set; } public LogErrorService _logger { get; set; } [SetUp] public void Setup() { var webHost = WebHost.CreateDefaultBuilder().ConfigureAppConfiguration((context, config) => { //https://stackoverflow.com/questions/38114761/asp-net-core-configuration-for-net-core-console-application IHostEnvironment env = context.HostingEnvironment; config.AddEnvironmentVariables() // copy configuration files to output directory .AddJsonFile("appsettings.json") // default prefix for environment variables is DOTNET_ .AddJsonFile($"appsettings.Development.json", optional: true); }) .UseStartup<Startup>() .Build(); _dependencyResolver = new DependencyResolver(webHost); var oService = _dependencyResolver.GetService<FileImageDataService>(); var oLogger = _dependencyResolver.GetService<LogErrorService>(); var oParam = _dependencyResolver.GetService<ParamAppService>(); var oConfig = _dependencyResolver.GetService<IConfiguration>(); var builder = new DbContextOptionsBuilder<FB_SERVICEContext>() .UseInMemoryDatabase("FB_SERVICE"); //var oContext = _dependencyResolver.GetService<FB_SERVICEContext>(); _context = new FB_SERVICEContext(builder.Options); _service = oService; _param = oParam; _logger = oLogger; _controller = new FileController(oService, oLogger, _param); } //[Test()] //public async Task PostTestAsync() //{ // var response = await _controller.GetAsync(); // Assert.Fail(); //} [Test()] public async Task GetByIdTestAsync() { var response = await _controller.DownloadFileByFileName("DIRECTSALES", "F8C22188-28CC-48CF-8C20-786B6BD15B70.jpg"); Assert.Fail(); } }
私が試したこと:
起動 :
services.AddDbContext<FB_SERVICEContext>(opt => opt.UseSqlServer(Configuration.GetSection("ConnectionString:SqlServer").Value));//.EnableSensitiveDataLogging(), ServiceLifetime.Transient); services.AddScoped<FileImageDataService>(); services.AddScoped<LogErrorService>(); services.AddScoped<ParamAppService>();
解決策 1
ドキュメントを確認することをお勧めします。 EF Core を使用するアプリケーションのテストの概要 – EF Core | マイクロソフト ラーン[^]
YouTube の説明ビデオを含む詳細情報はこちら: エンティティ フレームワークを使用した単体テスト – Google 検索[^]
[ad_2]
コメント