Recording allows information to be statically captured and then (optionally) verified.
The main value of this feature is to simplify addition of information to a snapshot from extensions to Verify. Before to this feature, for an extension to supply information to a snapshot, that extension had to return the information up the stack, and the calling test had to explicitly add that information to the Verify()
call. Using this feature an extension can Recording.Add()
in any context, and the information will be added to the snapshot.
[Fact]
public Task Usage()
{
Recording.Start();
Recording.Add("name", "value");
return Verify("TheValue");
}
Results in:
{
target: TheValue,
name: value
}
If Recording.Add()
is called before Recording.Start
an exception will be called.
Recording.TryAdd()
will add an item only if Recording.IsRecording
is true.
[Fact]
public Task TryAdd()
{
//using Recording.Add here would throw since Recording.Start has not been called
Recording.TryAdd("name1", "value1");
Recording.Start();
Recording.TryAdd("name2", "value2");
return Verify("TheValue");
}
Recording can be scoped via a using
:
[Fact]
public Task RecordingScoped()
{
using (Recording.Start())
{
Recording.Add("name1", "value1");
}
// Recording.Add is ignored here
Recording.Add("name2", "value2");
return Verify();
}
Results in:
{
name1: value1
}
Values are grouped by key:
[Fact]
public Task SameKey()
{
Recording.Start();
Recording.Add("name", "value1");
Recording.Add("name", "value2");
return Verify("TheValue");
}
Results in:
{
target: TheValue,
name: [
value1,
value2
]
}
To avoid grouping use Stop.
Recording can be grouped by an identifier.
The identifier should be statically unique. For example a fully qualified test name or a GUID.
[Fact]
public Task Identifier()
{
Recording.Start("identifier");
Recording.Add("identifier", "name", "value");
return Verify(Recording.Stop("identifier"));
}
Results in:
[
{
name: value
}
]
[Fact]
public Task Case()
{
Recording.Start();
Recording.Add("name", "value1");
Recording.Add("Name", "value2");
return Verify("TheValue");
}
Results in:
{
target: TheValue,
name: value1,
Name: value2
}
Recording can be stopped and the resulting data can be manually verified:
[Fact]
public Task Stop()
{
Recording.Start();
Recording.Add("name1", "value1");
Recording.Add("name2", "value2");
var appends = Recording.Stop();
return Verify(appends.Where(_ => _.Name != "name1"));
}
Results in:
[
{
name2: value2
}
]
If Stop is called, the results are not automatically verified:
[Fact]
public Task StopNotInResult()
{
Recording.Start();
Recording.Add("name1", "value1");
Recording.Add("name2", "value2");
Recording.Stop();
return Verify("other data");
}
Results in:
other data
The status of Recording can be checked.
[Fact]
public void IsRecording()
{
Assert.False(Recording.IsRecording());
Recording.Start();
Assert.True(Recording.IsRecording());
}
This can be helpful if the cost of capturing data, to add to recording, is high.
The current recorded items can be cleared:
[Fact]
public Task Clear()
{
Recording.Start();
Recording.Add("name1", "value1");
Recording.Clear();
Recording.Add("name2", "value2");
return Verify();
}
Results in:
{
name2: value2
}
Recording can be paused and resumed:
[Fact]
public Task PauseResume()
{
Recording.Start();
Recording.Pause();
Recording.Add("name1", "value1");
Recording.Resume();
Recording.Add("name2", "value2");
Recording.Pause();
Recording.Add("name3", "value3");
return Verify();
}
Results in:
{
name2: value2
}