Skip to content

Commit

Permalink
StrictMode(true) Fixing null reference exception (#339)
Browse files Browse the repository at this point in the history
Co-authored-by: bchavez <bchavez@bitarmory.com>
  • Loading branch information
Basyras and bchavez authored Dec 12, 2020
1 parent c429ae9 commit 8346c32
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Source/Bogus.Tests/StrictModeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,22 @@ public void cannot_use_rules_with_strictmode_inside_rulesets()
Action act2 = () => faker2.AssertConfigurationIsValid();
act2.Should().Throw<ValidationException>();
}

[Fact]
public void strictmode_with_no_rules_should_throw()
{
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.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 8346c32

Please sign in to comment.