Skip to content

Commit

Permalink
Added some tests and 0-arity func RuleFor
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Mar 23, 2016
1 parent c9ba2b8 commit b7b1a8a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v5.1.1-beta-3
* Removed RuleFor(x = x.Prop, constantValue), was confusing the API.
* Added 0-arity RuleFor(x = x.Prop, () => someValue)

## v5.1.1-beta-2
* Make f.UniqueIndex as int for convenience.
* Use generic RuleFor(x = x.Prop, constantValue).
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ var testUsers = new Faker<User>()
.RuleFor(u => u.UserName, (f, u) => f.Internet.UserName(u.FirstName, u.LastName))
.RuleFor(u => u.Email, (f, u) => f.Internet.Email(u.FirstName, u.LastName))
.RuleFor(u => u.SomethingUnique, f => $"Value {f.UniqueIndex}")
.RuleFor(u => u.SomethingConstant, "AConstantStringValue")

//Use an enum outside scope.
.RuleFor(u => u.Gender, f => f.PickRandom<Gender>())
Expand Down
5 changes: 3 additions & 2 deletions Source/Bogus.Tests/FluentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void TestAPIDesign()
.RuleFor(u => u.UserName, (f, u) => f.Internet.UserName(u.FirstName, u.LastName))
.RuleFor(u => u.Email, (f, u) => f.Internet.Email(u.FirstName, u.LastName))
.RuleFor(u => u.SomethingUnique, f => $"Value {f.UniqueIndex}")
.RuleFor(u => u.SomethingConstant, "SomeConstantStringValue")
.RuleFor(u => u.SomeGuid, Guid.NewGuid)

//Use an enum outside scope.
.RuleFor(u => u.Gender, f => f.PickRandom<Gender>())
Expand Down Expand Up @@ -177,8 +177,9 @@ public User(int userId, string ssn)
public string FullName { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string SomethingConstant { get; set; }
public string SomethingUnique { get; set; }
public Guid SomeGuid { get; set; }

public string Avatar { get; set; }
public Guid CartId { get; set; }
public string SSN { get; set; }
Expand Down
9 changes: 9 additions & 0 deletions Source/Bogus.Tests/RandomizerTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;
using FluentAssertions;
using NUnit.Framework;

Expand Down Expand Up @@ -69,6 +71,13 @@ public void can_get_some_random_words()
r.Words().Should().Be("Handcrafted Granite Gloves Directives");
r.Words().Should().Be("Corner Handcrafted Frozen Chair transmitting");
}

[Test]
public void can_shuffle_some_enumerable()
{
new string(r.Shuffle("123456789").ToArray())
.Should().Be("628753491");
}
}


Expand Down
14 changes: 14 additions & 0 deletions Source/Bogus.Tests/UniquenessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,19 @@ public void should_be_able_to_create_some_hash_ids()
ids.Should().BeEquivalentTo("gY", "jR", "k5", "l5", "mO");
}

[Test]
public void should_be_able_to_drive_manual_index()
{
int indexer = 0;
var faker = new Faker<User>()
.RuleFor(u => u.FirstName, f => f.Name.FirstName())
.RuleFor(u => u.LastName, f => new[] {"A", "B", "C", "D"}[indexer % 4])
.FinishWith((f, u) => indexer++);

var fakes = faker.Generate(10).ToList();

fakes.Dump();
}

}
}
8 changes: 3 additions & 5 deletions Source/Bogus/Faker[T].cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ public Faker<T> RuleFor<TProperty>(Expression<Func<T, TProperty>> property, Func
}

/// <summary>
/// RuleFor helper for constant string values.
/// Creates a rule for a property.
/// </summary>
/// <param name="constantValue">Constant string value used to set the property.</param>
/// <returns></returns>
public Faker<T> RuleFor<TProperty>(Expression<Func<T, TProperty>> property, TProperty constantValue)
public Faker<T> RuleFor<TProperty>(Expression<Func<T, TProperty>> property, Func<TProperty> valueFunction)
{
return RuleFor(property, (f) => constantValue);
return RuleFor(property, (f) => valueFunction());
}

/// <summary>
Expand Down

0 comments on commit b7b1a8a

Please sign in to comment.