Skip to content

Latest commit

 

History

History
370 lines (301 loc) · 11 KB

recording.md

File metadata and controls

370 lines (301 loc) · 11 KB

Recording

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.

Usage

[Fact]
public Task Usage()
{
    Recording.Start();
    Recording.Add("name", "value");
    return Verify("TheValue");
}

snippet source | anchor

Results in:

{
  target: TheValue,
  name: value
}

snippet source | anchor

TryAdd

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");
}

snippet source | anchor

Scoped

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();
}

snippet source | anchor

Results in:

{
  name1: value1
}

snippet source | anchor

Grouping

Values are grouped by key:

[Fact]
public Task SameKey()
{
    Recording.Start();
    Recording.Add("name", "value1");
    Recording.Add("name", "value2");
    return Verify("TheValue");
}

snippet source | anchor

Results in:

{
  target: TheValue,
  name: [
    value1,
    value2
  ]
}

snippet source | anchor

To avoid grouping use Stop.

Identifier

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"));
}

snippet source | anchor

Results in:

[
  {
    name: value
  }
]

snippet source | anchor

Case is ignored

[Fact]
public Task Case()
{
    Recording.Start();
    Recording.Add("name", "value1");
    Recording.Add("Name", "value2");
    return Verify("TheValue");
}

snippet source | anchor

Results in:

{
  target: TheValue,
  name: value1,
  Name: value2
}

snippet source | anchor

Stop

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"));
}

snippet source | anchor

Results in:

[
  {
    name2: value2
  }
]

snippet source | anchor

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");
}

snippet source | anchor

Results in:

other data

snippet source | anchor

IsRecording

The status of Recording can be checked.

[Fact]
public void IsRecording()
{
    Assert.False(Recording.IsRecording());
    Recording.Start();
    Assert.True(Recording.IsRecording());
}

snippet source | anchor

This can be helpful if the cost of capturing data, to add to recording, is high.

Clear

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();
}

snippet source | anchor

Results in:

{
  name2: value2
}

snippet source | anchor

Pause and Resume

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();
}

snippet source | anchor

Results in:

{
  name2: value2
}

snippet source | anchor

Extensions that leverage Recording