https://github.com/robsoncastilho/fluentbuilder
Library for dynamic creation of objects, implemented with a focus on writing more readable and less fragile unit tests.
https://github.com/robsoncastilho/fluentbuilder
csharp fluentbuilder nuget-package testdatabuilder
Last synced: about 1 month ago
JSON representation
Library for dynamic creation of objects, implemented with a focus on writing more readable and less fragile unit tests.
- Host: GitHub
- URL: https://github.com/robsoncastilho/fluentbuilder
- Owner: robsoncastilho
- License: mit
- Created: 2015-06-06T05:48:41.000Z (almost 10 years ago)
- Default Branch: main
- Last Pushed: 2024-10-07T02:13:50.000Z (8 months ago)
- Last Synced: 2025-04-09T14:06:37.155Z (about 2 months ago)
- Topics: csharp, fluentbuilder, nuget-package, testdatabuilder
- Language: C#
- Homepage:
- Size: 10 MB
- Stars: 17
- Watchers: 5
- Forks: 6
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://ci.appveyor.com/project/robsoncastilho/fluentbuilder-dnujh)
[](https://coveralls.io/github/robsoncastilho/FluentBuilder?branch=master)
[](https://www.nuget.org/packages/Nosbor.FluentBuilder/)
[](https://github.com/robsoncastilho/FluentBuilder/blob/master/LICENSE)
# FluentBuilderLibrary for dynamic creation of objects, implemented with a focus on writing more readable and less fragile unit tests.
### Available at NuGet
```powershell
Install-Package Nosbor.FluentBuilder
```### Compatibility
.Net Framework 4.5 or higher.
.Net Core 1.0
### Features
#### 1. Public API
##### 1.1. New
Creates a new FluentBuilder instance and starts the fluent construction of the destination object (see next sections).
##### 1.2. With
Allows setting values for writable properties (ie. properties must have 'set' with any kind of modifier):
```csharp
var newAddress = FluentBuilder
.New()
.With(a => a.Street, "1st Street")
.With(a => a.ZipCode, "1011")
.Build();
```##### 1.3. WithValue
Allows setting a value (reference type) just passing its instance. FluentBuilder will find the corresponding member to receive the instance based on naming convention:
```csharp
.New().Build())
var newCustomer = FluentBuilder
.New()
.WithValue(FluentBuilder
.Build();
```For code above succeed, Customer class must have a property or field named 'Address' or 'address' and being of the same type of Address (or a base class of Address).
##### 1.4. WithDependency
Allows setting values for a injected dependency stored in a private field.
This option allows creating a builder for a service that has dependencies and passing a test double object to the SUT.Sample 1 (Using a concrete dependency for integration tests):
```csharp
var concreteDependency = new SampleConcreteDependency();
var service = FluentBuilder
.New()
.WithDependency(concreteDependency)
.Build();
service.DoSomething();
......
```Sample 2 (Using a mock for unit tests):
Using Moq, but you can use another mocking library or manually implement your mock object.```csharp
var dependencyMock = new Mock();
var service = FluentBuilder
.New()
.WithDependency(dependencyMock.Object)
.Build();
service.DoSomething();dependencyMock.Verify(dependency => dependency.Do(), Times.Once);
......
```##### 1.5. AddingTo
Allows setting elements in a collection, one by one:
```csharp
var customer = FluentBuilder
.New()
.AddingTo(c => c.Addresses, new Address("1st Street"))
.AddingTo(c => c.Addresses, new Address("2nd Street"))
.Build();
```Or:
```csharp
.New().With(a => a.Street, "1st Street").Build())
var customer = FluentBuilder
.New()
.AddingTo(c => c.Addresses, FluentBuilder
.AddingTo(c => c.Addresses, FluentBuilder.New().With(a => a.Street, "2nd Street").Build())
.Build();
```The collection must be a read-only collection with a backing private field. If the collection is a writable property then you can set the whole collection using "With()".
##### 1.6. Build
Constructs and returns the expected object.
##### 1.7. AsList
Like Build() method, with the difference that AsList() builds and returns the object within a list:
```csharp
addresses = FluentBuilder
IEnumerable
.New()
.With(a => a.Street, "1st Street")
.AsList();
```It's helpful when just one object is necessary for the test but the method/construtor of the SUT requires a collection of objects of that type.
#### 2. Implicit conversion
Allows calling Build() to be avoided since you use the returned type explicitly instead of 'var':
```csharp
.New().With(a => a.Street, "1st Street");
Address address = FluentBuilder
```