Skip to content

Commit

Permalink
Allow multiple calls to FinishWith. Re: #115.
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Dec 16, 2017
1 parent de8c62b commit bee5ab4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## v21.0.5
* Better error support.
* Added `Person.FullName` field.
* Allowed `Faker<T>.FinishWith` to be called multiple times. Last call wins.

## v21.0.4
* Fixed `f.Image` URL generation.
Expand Down
40 changes: 39 additions & 1 deletion Source/Bogus.Tests/GitHubIssues/Issue115.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
using Bogus.Tests.Models;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

namespace Bogus.Tests.GitHubIssues
{
public class Issue115
public class Issue115 : SeededTest
{
private readonly ITestOutputHelper console;

public Issue115(ITestOutputHelper console)
{
this.console = console;
}

public class Customer
{
public string Name { get; set; }
Expand All @@ -23,5 +31,35 @@ public void should_throw_with_nested_expression()
fakerMaker.ShouldThrow<ArgumentException>();

}

[Fact]
public void calling_finish_with_twice_is_okay()
{
var productCalled = false;
var colorCalled = false;

var faker = new Faker<Order>()
.RuleFor(o => o.OrderId, f => f.Random.Number(1,50))
.FinishWith((f, o) =>
{
productCalled = true;
o.Item = f.Commerce.Product();
})
.FinishWith((f, o) =>
{
colorCalled = true;
o.Item = f.Commerce.Color();
});

var order = faker.Generate();

order.Item.Should().Be("yellow");

//sanity check
productCalled.Should().BeFalse();
colorCalled.Should().BeTrue();

console.Dump(order);
}
}
}
2 changes: 1 addition & 1 deletion Source/Bogus/Faker[T].cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public virtual Faker<T> FinishWith(Action<Faker, T> action)
Action = action,
RuleSet = currentRuleSet
};
this.FinalizeActions.Add(currentRuleSet, rule);
this.FinalizeActions[currentRuleSet] = rule;
return this;
}

Expand Down

0 comments on commit bee5ab4

Please sign in to comment.