FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [View Raw Code]   [Original HTTPS Page]

Verify/docs/guids.md at main · VerifyTests/Verify · GitHub

Latest commit

 

History

History
262 lines (207 loc) · 8.93 KB

File metadata and controls

262 lines (207 loc) · 8.93 KB

Guids

By default guids are sanitized during verification. This is done by finding each guid and taking a counter based that that specific guid. That counter is then used replace the guid values. This allows for repeatable tests when guid values are changing.

var guid = Guid.NewGuid();
var target = new GuidTarget
{
    Guid = guid,
    GuidNullable = guid,
    GuidString = guid.ToString(),
    OtherGuid = Guid.NewGuid()
};

await Verify(target);

snippet source | anchor

Results in the following:

{
  Guid: Guid_1,
  GuidNullable: Guid_1,
  GuidString: Guid_1,
  OtherGuid: Guid_2
}

snippet source | anchor

Disable

To disable this behavior use:

Instance

var settings = new VerifySettings();
settings.DontScrubGuids();
await Verify(target, settings);

snippet source | anchor

Fluent

await Verify(target)
    .DontScrubGuids();

snippet source | anchor

Globally

VerifierSettings.DontScrubGuids();

snippet source | anchor

Inline Guids

Strings containing inline Guids can also be scrubbed. Guids in the D format (00000000-0000-0000-0000-000000000000) and the N format (00000000000000000000000000000000) are matched when bounded by non-alphanumeric characters. B and P formatted Guids are covered by the D format matching between their delimiters. Note that any 32 character hex sequence is a valid N format Guid, so hex content of that exact length (an MD5 hash for example) will also be scrubbed.

To enable this behavior, use:

Instance

[Fact]
public Task ScrubInlineGuidsInstance()
{
    var settings = new VerifySettings();
    settings.ScrubInlineGuids();
    return Verify(
        "content 651ad409-fc30-4b12-a47e-616d3f953e4c content",
        settings);
}

snippet source | anchor

Fluent

[Fact]
public Task ScrubInlineGuidsFluent() =>
    Verify("content 651ad409-fc30-4b12-a47e-616d3f953e4c content")
        .ScrubInlineGuids();

snippet source | anchor

Globally

public static class ModuleInitializer
{
    [ModuleInitializer]
    public static void Init() =>
        VerifierSettings.ScrubInlineGuids();
}

snippet source | anchor

Limiting to specific formats

By default both the D and N formats are scrubbed. To restrict scrubbing to specific formats, pass a GuidFormats value. This is useful for avoiding the scrubbing of 32 character hex content (an MD5 hash for example) that would otherwise match the N format:

// Only the "D" format is scrubbed. The 32 char hex hash is left untouched.
[Fact]
public Task ScrubInlineGuidsDashedOnly() =>
    Verify("guid: 173535ae-995b-4cc6-a74e-8cd4be57039c hash: 5d41402abc4b2a76b9719d911017c592")
        .ScrubInlineGuids(GuidFormats.Dashed);

snippet source | anchor

Results in the following, where the D format Guid is scrubbed but the hash is left untouched:

guid: Guid_1 hash: 5d41402abc4b2a76b9719d911017c592

snippet source | anchor

Named Guid

Specific Guids can be named. When any of those Guids are found, it will be replaced with the supplied name.

Instance

[Fact]
public Task NamedGuidInstance()
{
    var settings = new VerifySettings();
    var guid = new Guid("c8eeaf99-d5c4-4341-8543-4597c3fd40d9");
    settings.AddNamedGuid(guid, "instanceNamed");
    return Verify(
        new
        {
            value = guid
        },
        settings);
}

snippet source | anchor

Instance

[Fact]
public Task NamedGuidFluent()
{
    var guid = new Guid("c8eeaf99-d5c4-4341-8543-4597c3fd40d9");
    return Verify(
            new
            {
                value = guid
            })
        .AddNamedGuid(guid, "instanceNamed");
}

snippet source | anchor

Globally

[ModuleInitializer]
public static void Init() => VerifierSettings.AddNamedGuid(new("c8eeaf99-d5c4-4341-8543-4597c3fd40c9"), "guidName");

snippet source | anchor

Inferred Name

The name can be inferred from the variable name by omitting the name argument:

[Fact]
public Task InferredNamedGuidFluent()
{
    var namedGuid = new Guid("c8eeaf99-d5c4-4341-8543-4597c3fd40d9");
    return Verify(
            new
            {
                value = namedGuid
            })
        .AddNamedGuid(namedGuid);
}

snippet source | anchor

Result:

{
  value: namedGuid
}

snippet source | anchor


Back | FazBrowse Home | New Git URL