-
Notifications
You must be signed in to change notification settings - Fork 2
Snapshot testing
Snapshot testing, is a test that creates an expected output, usually a file.
This file will be compared to an accepted file output.
If the accepted file is the same as the newly created file - the snapshot test succeeds.
On first run, there will not exist any accepted file.
On the first run the newly created output should be inspected if it is correct.
If the output is as expected, the newly created file should be marked as an accepted file.
On the second run, the test should succeed.
A test method:
[Fact]
public Task Custom_class_name()
{
var source = """
using PartialSourceGen;
namespace MySpace;
/// <summary>
/// Class:
/// [Partial(PartialClassName = "ModelPartialCustom")]
/// public class Model
/// </summary>
[Partial(PartialClassName = "ModelPartialCustom")]
public class Model
{
/// <summary>
/// input:
/// public string Name { get; set; }
/// </summary>
public string Name { get; set; }
}
""";
var runResult = TestHelper.GeneratorDriver(source)
.GetRunResult()
.GetSecondResult();
var settings = Settings();
return Verify(runResult, settings).UseDirectory("Results/Classes");
}Will produce 2 files:
- Received:
Custom_class_name#ModelPartialCustom.g.received.cs - Verified:
Custom_class_name#ModelPartialCustom.g.verified.cs
The received file will be the newly created output, this will contain the new content.
The verified file will be the accepted output, this will be empty.
The test will fail 🔴.
You must then manually copy-paste the content from received to verified, after accepting the received output.
After running the test a 2nd time, the received file will automatically be deleted.
The test will succeed 🟢.