parent
589a4bd413
commit
a014509135
@ -0,0 +1 @@
|
||||
global using Xunit;
|
@ -0,0 +1,48 @@
|
||||
using DbContextLib;
|
||||
using Entities;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
namespace UnitTests
|
||||
{
|
||||
public class UnitTest1
|
||||
{
|
||||
[Fact]
|
||||
public void Add_Test()
|
||||
{
|
||||
//connection must be opened to use In-memory database
|
||||
var connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
|
||||
var options = new DbContextOptionsBuilder<LibraryContext>.UseSqlLite().options;
|
||||
|
||||
//prepares the database with one instance of the context
|
||||
using (var context = new LibraryContext(options))
|
||||
{
|
||||
//context.Database.OpenConnection();
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
BookEntity the100 = new BookEntity( "the100", "", "" );
|
||||
BookEntity princeOfPersia = new BookEntity ( "princeOfPersia", "", "" );
|
||||
BookEntity PercyJackson = new BookEntity ("PercyJackson", "", "" );
|
||||
context.BooksSet.Add(the100);
|
||||
context.BooksSet.Add(princeOfPersia);
|
||||
context.BooksSet.Add(PercyJackson);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
//uses another instance of the context to do the tests
|
||||
using (var context = new LibraryContext(options))
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
Assert.Equal(3, context.BooksSet.Count());
|
||||
Assert.Equal("the100", context.BooksSet.First().Title);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
|
||||
<ProjectReference Include="..\Entities\Entities.csproj" />
|
||||
<ProjectReference Include="..\StubbedContextLib\StubbedContextLib.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Loading…
Reference in new issue