Skip to content

Commit

Permalink
Update unit test and use C# 9 feature for null checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Dec 12, 2020
1 parent cc45fa2 commit 9339cb8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 13 additions & 7 deletions Source/Bogus.Tests/StrictModeTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Bogus.Tests.Models;
using FluentAssertions;
using Xunit;

Expand Down Expand Up @@ -97,13 +96,20 @@ public void cannot_use_rules_with_strictmode_inside_rulesets()
}

[Fact]
public void StrictMode_True_NoRules()
public void strictmode_with_no_rules_should_throw()
{
Assert.Throws<ValidationException>(() =>
{
new Faker<Order>()
.StrictMode(true).Generate(1);
});
var faker = new Faker<Examples.Order>()
.StrictMode(true);

Action act = () => faker.Generate(1);

act.Should().ThrowExactly<ValidationException>()
.WithMessage("*Missing Rules*")
.WithMessage("*Validation was called to ensure all properties*")
.WithMessage($"*{nameof(Examples.Order.OrderId)}*")
.WithMessage($"*{nameof(Examples.Order.Item)}*")
.WithMessage($"*{nameof(Examples.Order.Quantity)}*")
.WithMessage($"*{nameof(Examples.Order.LotNumber)}*");
}
}
}
2 changes: 1 addition & 1 deletion Source/Bogus/Faker[T].cs
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ private ValidationResult ValidateInternal(string[] ruleSets)
{
foreach( var propOrFieldOfT in userSet )
{
if( populateActions != null && populateActions.TryGetValue(propOrFieldOfT, out var populateAction) )
if( populateActions is not null && populateActions.TryGetValue(propOrFieldOfT, out var populateAction) )
{
// Very much a .Rules() action
if( populateAction.ProhibitInStrictMode )
Expand Down

0 comments on commit 9339cb8

Please sign in to comment.