{"id":15011687,"url":"https://github.com/robsoncastilho/fluentbuilder","last_synced_at":"2025-04-12T03:31:40.812Z","repository":{"id":33324353,"uuid":"36969095","full_name":"robsoncastilho/FluentBuilder","owner":"robsoncastilho","description":"Library for dynamic creation of objects, implemented with a focus on writing more readable and less fragile unit tests.","archived":false,"fork":false,"pushed_at":"2024-10-07T02:13:50.000Z","size":10487,"stargazers_count":17,"open_issues_count":9,"forks_count":6,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-09T14:06:37.155Z","etag":null,"topics":["csharp","fluentbuilder","nuget-package","testdatabuilder"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robsoncastilho.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-06T05:48:41.000Z","updated_at":"2024-10-07T02:09:34.000Z","dependencies_parsed_at":"2022-09-12T19:11:41.450Z","dependency_job_id":null,"html_url":"https://github.com/robsoncastilho/FluentBuilder","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robsoncastilho%2FFluentBuilder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robsoncastilho%2FFluentBuilder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robsoncastilho%2FFluentBuilder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robsoncastilho%2FFluentBuilder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robsoncastilho","download_url":"https://codeload.github.com/robsoncastilho/FluentBuilder/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248512723,"owners_count":21116667,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","fluentbuilder","nuget-package","testdatabuilder"],"created_at":"2024-09-24T19:41:26.804Z","updated_at":"2025-04-12T03:31:40.563Z","avatar_url":"https://github.com/robsoncastilho.png","language":"C#","readme":"[![Build status](https://ci.appveyor.com/api/projects/status/mtrlfipo9nprindl?svg=true)](https://ci.appveyor.com/project/robsoncastilho/fluentbuilder-dnujh)\n[![Coverage Status](https://coveralls.io/repos/robsoncastilho/FluentBuilder/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/robsoncastilho/FluentBuilder?branch=master)\n[![NuGet Badge](https://buildstats.info/nuget/Nosbor.FluentBuilder)](https://www.nuget.org/packages/Nosbor.FluentBuilder/)\n[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/robsoncastilho/FluentBuilder/blob/master/LICENSE)\n# FluentBuilder\n\nLibrary for dynamic creation of objects, implemented with a focus on writing more readable and less fragile unit tests.\n\n### Available at NuGet\n```powershell\nInstall-Package Nosbor.FluentBuilder\n```\n\n### Compatibility\n\n.Net Framework 4.5 or higher.\n\n.Net Core 1.0\n\n### Features\n\n#### 1. Public API\n\n##### 1.1. New\n\nCreates a new FluentBuilder instance and starts the fluent construction of the destination object (see next sections).\n\n##### 1.2. With\n\nAllows setting values for writable properties (ie. properties must have 'set' with any kind of modifier):\n\n```csharp\nvar newAddress = FluentBuilder\u003cAddress\u003e\n                .New()\n                .With(a =\u003e a.Street, \"1st Street\")\n                .With(a =\u003e a.ZipCode, \"1011\")\n                .Build();\n```\n\n##### 1.3. WithValue\n\nAllows setting a value (reference type) just passing its instance. FluentBuilder will find the corresponding member to receive the instance based on naming convention:\n\n```csharp\nvar newCustomer = FluentBuilder\u003cCustomer\u003e\n                  .New()\n                  .WithValue(FluentBuilder\u003cAddress\u003e.New().Build())\n                  .Build();\n```\n\nFor 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).\n\n##### 1.4. WithDependency\n\nAllows setting values for a injected dependency stored in a private field.\nThis option allows creating a builder for a service that has dependencies and passing a test double object to the SUT.\n\nSample 1 (Using a concrete dependency for integration tests):\n\n```csharp\nvar concreteDependency = new SampleConcreteDependency();\nvar service = FluentBuilder\u003cSampleServiceWithDependency\u003e\n              .New()\n              .WithDependency\u003cIDependency, SampleConcreteDependency\u003e(concreteDependency)\n              .Build();\n    \nservice.DoSomething();\n......\n```\n\nSample 2 (Using a mock for unit tests):\nUsing Moq, but you can use another mocking library or manually implement your mock object.\n\n```csharp\nvar dependencyMock = new Mock\u003cIDependency\u003e();\nvar service = FluentBuilder\u003cSampleServiceWithDependency\u003e\n              .New()\n              .WithDependency\u003cIDependency, IDependency\u003e(dependencyMock.Object)\n              .Build();\n    \nservice.DoSomething();\n\ndependencyMock.Verify(dependency =\u003e dependency.Do(), Times.Once);\n......\n```\n\n##### 1.5. AddingTo\n\nAllows setting elements in a collection, one by one:\n\n```csharp\nvar customer = FluentBuilder\u003cCustomer\u003e\n              .New()\n              .AddingTo(c =\u003e c.Addresses, new Address(\"1st Street\"))\n              .AddingTo(c =\u003e c.Addresses, new Address(\"2nd Street\"))\n              .Build();\n```\n\nOr:\n\n```csharp\nvar customer = FluentBuilder\u003cCustomer\u003e\n              .New()\n              .AddingTo(c =\u003e c.Addresses, FluentBuilder\u003cAddress\u003e.New().With(a =\u003e a.Street, \"1st Street\").Build())\n              .AddingTo(c =\u003e c.Addresses, FluentBuilder\u003cAddress\u003e.New().With(a =\u003e a.Street, \"2nd Street\").Build())\n              .Build();\n```\n\nThe 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()\".\n\n##### 1.6. Build\n\nConstructs and returns the expected object.\n\n##### 1.7. AsList\n\nLike Build() method, with the difference that AsList() builds and returns the object within a list:\n\n```csharp\nIEnumerable\u003cAddress\u003e addresses = FluentBuilder\u003cAddress\u003e\n                                .New()\n                                .With(a =\u003e a.Street, \"1st Street\")\n                                .AsList();\n```\n\nIt'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.\n\n#### 2. Implicit conversion\n\nAllows calling Build() to be avoided since you use the returned type explicitly instead of 'var':\n\n```csharp\nAddress address = FluentBuilder\u003cAddress\u003e.New().With(a =\u003e a.Street, \"1st Street\");\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobsoncastilho%2Ffluentbuilder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobsoncastilho%2Ffluentbuilder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobsoncastilho%2Ffluentbuilder/lists"}