This commit is contained in:
Simon Cropp 2023-10-28 22:12:55 +11:00
Родитель bec58fa465
Коммит dccab152d2
2 изменённых файлов: 53 добавлений и 45 удалений

Просмотреть файл

@ -77,7 +77,7 @@ builder.UseSqlServer(connection);
builder.EnableRecording();
var data = new SampleDbContext(builder.Options);
```
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L294-L301' title='Snippet source file'>snippet source</a> | <a href='#snippet-enablerecording' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L296-L303' title='Snippet source file'>snippet source</a> | <a href='#snippet-enablerecording' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
`EnableRecording` should only be called in the test context.
@ -105,7 +105,7 @@ await data.Companies
await Verify(data.Companies.Count());
```
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L391-L408' title='Snippet source file'>snippet source</a> | <a href='#snippet-recording' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L393-L410' title='Snippet source file'>snippet source</a> | <a href='#snippet-recording' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
Will result in the following verified file:
@ -158,13 +158,14 @@ await data.Companies
var entries = EfRecording.FinishRecording();
//TODO: optionally filter the results
await Verify(new
{
target = data.Companies.Count(),
sql = entries
});
await Verify(
new
{
target = data.Companies.Count(),
sql = entries
});
```
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L509-L532' title='Snippet source file'>snippet source</a> | <a href='#snippet-recordingspecific' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L513-L537' title='Snippet source file'>snippet source</a> | <a href='#snippet-recordingspecific' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
@ -195,7 +196,7 @@ await data2.Companies
await Verify(data2.Companies.Count());
```
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L360-L382' title='Snippet source file'>snippet source</a> | <a href='#snippet-multidbcontexts' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L362-L384' title='Snippet source file'>snippet source</a> | <a href='#snippet-multidbcontexts' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
<!-- snippet: CoreTests.MultiDbContexts.verified.txt -->
@ -389,7 +390,7 @@ var queryable = data.Companies
.Where(_ => _.Content == "value");
await Verify(queryable);
```
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L253-L259' title='Snippet source file'>snippet source</a> | <a href='#snippet-queryable' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L254-L260' title='Snippet source file'>snippet source</a> | <a href='#snippet-queryable' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
Will result in the following verified file:
@ -447,7 +448,7 @@ await Verify(data.AllData())
serializer =>
serializer.TypeNameHandling = TypeNameHandling.Objects);
```
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L232-L239' title='Snippet source file'>snippet source</a> | <a href='#snippet-alldata' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L233-L240' title='Snippet source file'>snippet source</a> | <a href='#snippet-alldata' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
Will result in the following verified file with all data in the database:
@ -560,10 +561,11 @@ protected override void ConfigureWebHost(IWebHostBuilder webBuilder)
.EnableRecording(name)
.UseSqlite($"Data Source={name};Mode=Memory;Cache=Shared");
webBuilder.ConfigureTestServices(
_ => _.AddScoped(_ => dataBuilder.Options));
_ => _.AddScoped(
_ => dataBuilder.Options));
}
```
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L467-L478' title='Snippet source file'>snippet source</a> | <a href='#snippet-enablerecordingwithidentifier' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L470-L482' title='Snippet source file'>snippet source</a> | <a href='#snippet-enablerecordingwithidentifier' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
Then use the same identifier for recording:
@ -579,7 +581,7 @@ var companies = await httpClient.GetFromJsonAsync<Company[]>("/companies");
var entries = EfRecording.FinishRecording(testName);
```
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L441-L451' title='Snippet source file'>snippet source</a> | <a href='#snippet-recordwithidentifier' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L443-L453' title='Snippet source file'>snippet source</a> | <a href='#snippet-recordwithidentifier' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
The results will not be automatically included in verified file so it will have to be verified manually:
@ -587,13 +589,14 @@ The results will not be automatically included in verified file so it will have
<!-- snippet: VerifyRecordedCommandsWithIdentifier -->
<a id='snippet-verifyrecordedcommandswithidentifier'></a>
```cs
await Verify(new
{
target = companies!.Length,
sql = entries
});
await Verify(
new
{
target = companies!.Length,
sql = entries
});
```
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L453-L461' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyrecordedcommandswithidentifier' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L455-L464' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyrecordedcommandswithidentifier' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Просмотреть файл

@ -195,10 +195,11 @@ public class CoreTests
[Test]
public Task ShouldIgnoreDbContext() =>
Verify(new
{
Factory = new SampleDbContext(new DbContextOptions<SampleDbContext>())
});
Verify(
new
{
Factory = new SampleDbContext(new DbContextOptions<SampleDbContext>())
});
class MyDbContextFactory : IDbContextFactory<SampleDbContext>
{
@ -282,10 +283,11 @@ public class CoreTests
var data = database.Context;
var queryable = data.Companies
.Where(_ => _.Content == "value");
await Verify(new
{
queryable
});
await Verify(
new
{
queryable
});
}
// ReSharper disable once UnusedVariable
@ -452,11 +454,12 @@ public class CoreTests
#region VerifyRecordedCommandsWithIdentifier
await Verify(new
{
target = companies!.Length,
sql = entries
});
await Verify(
new
{
target = companies!.Length,
sql = entries
});
#endregion
}
@ -472,31 +475,32 @@ public class CoreTests
.EnableRecording(name)
.UseSqlite($"Data Source={name};Mode=Memory;Cache=Shared");
webBuilder.ConfigureTestServices(
_ => _.AddScoped(_ => dataBuilder.Options));
_ => _.AddScoped(
_ => dataBuilder.Options));
}
#endregion
protected override IHostBuilder CreateHostBuilder() =>
Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder.UseStartup<Startup>());
.ConfigureWebHostDefaults(_ => _.UseStartup<Startup>());
}
public class Startup
{
public void ConfigureServices(IServiceCollection services) =>
services
.AddDbContext<SampleDbContext>(builder =>
{
builder.UseInMemoryDatabase("");
});
.AddDbContext<SampleDbContext>(
_ => _.UseInMemoryDatabase(""));
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseEndpoints(endpoints
=> endpoints.MapGet("/companies", (SampleDbContext data) => data.Companies.ToListAsync()));
=> endpoints.MapGet(
"/companies",
(SampleDbContext data) => data.Companies.ToListAsync()));
}
}
@ -523,11 +527,12 @@ public class CoreTests
var entries = EfRecording.FinishRecording();
//TODO: optionally filter the results
await Verify(new
{
target = data.Companies.Count(),
sql = entries
});
await Verify(
new
{
target = data.Companies.Count(),
sql = entries
});
#endregion
}