Skip to content

Commit

Permalink
Add directive when mocking DateTimes and Guids
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswalpen committed Sep 6, 2021
1 parent 1a98041 commit e56e51b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
6 changes: 5 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ nav_order: 99
---

## Polaroider Changelog
### 2.0.x
### 2.0.2
- Mocking Guids also adds a directive to replace string Guids
- Mocking DateTimes also adds a directive to replace ISO 8601 string DateTimes

### 2.0.1
- Mocking dynamic data
- Indexers are ignored when mapping properties
- Map only properties that have getters
Expand Down
12 changes: 11 additions & 1 deletion src/Polaroider/SnapshotOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public static SnapshotOptions UseBasicFormatters(this SnapshotOptions options)

/// <summary>
/// Mocks changing <see cref="DateTime"/> values to a compareable format "0000-00-00T00:00:00.0000". Else the Snapshot would always fail due to changes in the time.
/// This simply adds a <see cref="MockDateTimeFormatter"/> to the options.
/// This adds a <see cref="MockDateTimeFormatter"/> and a directive to the options.
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
Expand All @@ -276,15 +276,25 @@ public static SnapshotOptions MockDateTimes(this SnapshotOptions options)
options.AddFormatter(typeof(DateTime), formatter);
options.AddFormatter(typeof(DateTime?), formatter);

options.AddDirective(d => d.ReplaceDateTime());

return options;
}

/// <summary>
/// Mocks a <see cref="Guid"/> values to a compareable default format "00000000-0000-0000-0000-000000000000". Else the Snapshot would always fail due to changes in the Guid.
/// This adds a <see cref="MockGuidFormatter"/> and a directive to the options.
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
public static SnapshotOptions MockGuids(this SnapshotOptions options)
{
var formatter = new MockGuidFormatter();
options.AddFormatter(typeof(Guid), formatter);
options.AddFormatter(typeof(Guid?), formatter);

options.AddDirective(d => d.ReplaceGuid());

return options;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public void Mapper_MockDateTime_Tokenize_Nullable_Nullable_Null()
new { Value = (DateTime?)null }.Tokenize(options).ToString().Should().Be("Value: ");
}

[Test]
public void Mapper_MockDateTime_ISOStringDate()
{
var options = SnapshotOptions.Create(o => o.MockDateTimes());
new { Value = DateTime.Now.ToString("o") }.Tokenize(options).ToString().Should().Be("Value: 0000-00-00T00:00:00.0000");
}

[Test]
public void Mapper_MockGuids_SnapshotOptions_Tokenize()
{
Expand All @@ -76,5 +83,12 @@ public void Mapper_MockGuid_Tokenize_Nullable_Nullable_Null()
var options = SnapshotOptions.Create(o => o.MockGuids());
new { Value = (Guid?)null }.Tokenize(options).ToString().Should().Be("Value: ");
}

[Test]
public void Mapper_MockGuids_StringFuid()
{
var options = SnapshotOptions.Create(o => o.MockGuids());
new { Value = Guid.NewGuid().ToString() }.Tokenize(options).ToString().Should().Be("Value: 00000000-0000-0000-0000-000000000000");
}
}
}

0 comments on commit e56e51b

Please sign in to comment.