gpt4 book ai didi

c# - 在Docker容器中运行SQLite内存中单元测试时出错

转载 作者:行者123 更新时间:2023-12-02 18:26:27 25 4
gpt4 key购买 nike

我正在使用docker容器在Azure管道中运行XUnit测试。对于每个.NET Core单元测试项目,我都有一个Dockerfile。我遵循此处详述的模式:

Running your unit tests with Visual Studio Team Services and Docker Compose

我能够运行所有单元测试项目,但我使用以下引用的项目除外:

Microsoft.EntityFrameworkCore.Sqlite
Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite。

我在内存中使用SQLite。

var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();

var option = new DbContextOptionsBuilder<Context>()
.UseSqlite(connection,
s => {
s.UseNetTopologySuite();
}).Options;

var dbContext = new Context(option, null);

最初,我将DockerFile设置如下:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Infrastructure.Tests
RUN dotnet restore

但是,在镜像中构建和运行时,我收到以下错误:

“无法加载共享库'libsqlite3-mod-spatialite'或其依赖项之一。”

单元测试可以在Visual Studio测试运行器中正常运行,而不能在图像中运行。

经过研究,我更改了Dockerfile以安装spacespacelite。
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Infrastructure.Tests
RUN dotnet restore

RUN apt-get update && apt-get install -y \
libsqlite3-mod-spatialite

我收到以下新错误:

Activity 的测试运行已中止。原因:测试主机进程崩溃。

在将SQLite与空间数据结合使用时,我尝试按照Microsoft的建议创建自定义SQLitePCLRaw提供程序。

Microsoft Documentation on Spatial Data
public class NativeLibraryAdapter : IGetFunctionPointer
{
readonly IntPtr _library;

public NativeLibraryAdapter(string name)
=> _library = NativeLibrary.Load(name);

public IntPtr GetFunctionPointer(string name)
=> NativeLibrary.TryGetExport(_library, name, out var address)
? address
: IntPtr.Zero;
}

And in my SQLite configuration:



SQLite3Provider_dynamic_cdecl
.Setup("sqlite3", new NativeLibraryAdapter("sqlite3"));

SQLitePCL.raw.SetProvider(new SQLite3Provider_dynamic_cdecl());

var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();

var option = new DbContextOptionsBuilder<EmployeeContext>()
.UseSqlite(connection,
s => {
s.UseNetTopologySuite();
}).Options;

现在,我收到以下错误:
“无法加载共享库'sqlite3'或其依赖项之一。”

这在Visual Studio测试运行程序和运行Docker镜像时都会发生。

在这一点上,我不确定我是否采用正确的方法来使它起作用。任何指导表示赞赏。

最佳答案

使用mcr.microsoft.com/dotnet/core/sdk:3.1时,我遇到了类似的问题。为了在测试案例中运行SaptiaLite,我必须安装以下两个软件包:

apt-get -y --no-install-recommends install libsqlite3-dev libsqlite3-mod-spatialite
我必须使用Microsoft而不是Microsoft.EntityFrameworkCore.Sqlite
  • Microsoft.EntityFrameworkCore.Sqlite.Core
  • Microsoft.Data.Sqlite.Core
  • SQLitePCLRaw.provider.sqlite3

  • 这些软件包使用系统提供的SQLite。
    Dockerfile和示例代码可以在这里找到:
    https://bitbucket.org/assetic/pipelines-dotnet-sonar-spatialite/src/master/

    关于c# - 在Docker容器中运行SQLite内存中单元测试时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62108424/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com