.NET MAUI集成测试:FileNotFoundException无法加载Microsoft.Maui.Essentials
在对使用Entity Framework Core的.NET MAUI应用进行集成测试时,执行到this.Database.EnsureCreated()时抛出文件未找到异常:
System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.Maui.Essentials, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'
尝试安装Microsoft.Maui.Essentials.Ref系列包时,收到错误:
'The package Microsoft.Maui.Essentials.Ref.any 6.0.547 has a package type DotnetPlatform that is incompatible with this project'
相关代码和项目配置如下:
集成测试代码
... var options = new DbContextOptionsBuilder<DataContext>() .UseInMemoryDatabase(databaseName: "Test_Database") .Options; var mockDataContext = new Mock<DataContext>(options); mockDataContextCreator .Setup(x => x.CreateDbContext()) .Returns(new DataContext(options)); ...
DataContext代码
public class DataContext : DbContext ... public DataContext(DbContextOptions options) : base(options) { SQLitePCL.Batteries_V2.Init(); if (!this.Database.EnsureCreated()) { this.Database.Migrate(); } }
集成测试项目CSPROJ配置
... <PropertyGroup> <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <IsPackable>false</IsPackable> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0" /> <PackageReference Include="Microsoft.Maui.Dependencies" Version="6.0.547" /> <PackageReference Include="Microsoft.Maui.Extensions" Version="6.0.547" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> <PackageReference Include="Moq" Version="4.18.2" /> <PackageReference Include="NUnit" Version="3.13.3" /> <PackageReference Include="NUnit3TestAdapter" Version="4.2.1" /> <PackageReference Include="NUnit.Analyzers" Version="3.3.0" /> <PackageReference Include="coverlet.collector" Version="3.1.2" /> </ItemGroup> ...
1. 移除测试项目中不必要的MAUI依赖
测试项目为net6.0常规类库,无需引用MAUI专用的Microsoft.Maui.Dependencies和Microsoft.Maui.Extensions包,直接从CSPROJ中删除这两个引用,避免依赖冲突:
<!-- 移除以下两行引用 --> <PackageReference Include="Microsoft.Maui.Dependencies" Version="6.0.547" /> <PackageReference Include="Microsoft.Maui.Extensions" Version="6.0.547" />
2. 分离SQLite初始化逻辑,避免测试环境触发
SQLitePCL.Batteries_V2.Init()是SQLite数据库专用的初始化代码,而测试使用的是内存数据库,完全不需要这一步。将初始化逻辑移到MAUI项目的启动代码中(而非DataContext构造函数):
修改MAUI项目的Program.cs
var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); }); // 在MAUI启动时初始化SQLite SQLitePCL.Batteries_V2.Init(); // 注册DataContext等服务 builder.Services.AddDbContext<DataContext>(options => options.UseSqlite("Data Source=MyDatabase.db")); var app = builder.Build(); app.Run();
修改DataContext构造函数
移除SQLite初始化代码,简化构造逻辑:
public DataContext(DbContextOptions options) : base(options) { if (!this.Database.EnsureCreated()) { this.Database.Migrate(); } }
3. 统一EF Core版本(可选但推荐)
测试项目使用的Microsoft.EntityFrameworkCore.InMemory版本为7.0.0,而MAUI项目基于net6.0,建议统一使用net6.0对应的EF Core版本(6.x系列),避免跨版本兼容性问题:
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.25" />
4. 简化测试代码(可选)
测试中无需Mock DataContext,直接实例化即可,内存数据库本身是轻量级的,不会有额外开销:
var options = new DbContextOptionsBuilder<DataContext>() .UseInMemoryDatabase(databaseName: "Test_Database") .Options; // 直接实例化DataContext,无需Mock var dataContext = new DataContext(options); mockDataContextCreator .Setup(x => x.CreateDbContext()) .Returns(dataContext);
内容的提问来源于stack exchange,提问作者Scott Seto

