Coder Social home page Coder Social logo

wellguimaraes / fluentbuilder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from robsoncastilho/fluentbuilder

0.0 2.0 0.0 10.12 MB

Library for dynamic creation of objects, implemented with a focus on writing more readable and less fragile unit tests.

C# 100.00%

fluentbuilder's Introduction

Build status Coverage Status NuGet Badge

FluentBuilder

Library for dynamic creation of objects, implemented with a focus on writing more readable and less fragile unit tests.

Available at NuGet

Install-Package Nosbor.FluentBuilder

Compatibility

.Net Framework 4.5 or higher.

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):

var newAddress = FluentBuilder<Address>
                .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:

var newCustomer = FluentBuilder<Customer>
                  .New()
                  .WithValue(FluentBuilder<Address>.New().Build())
                  .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):

var concreteDependency = new SampleConcreteDependency();
var service = FluentBuilder<SampleServiceWithDependency>
              .New()
              .WithDependency<IDependency, SampleConcreteDependency>(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.

var dependencyMock = new Mock<IDependency>();
var service = FluentBuilder<SampleServiceWithDependency>
              .New()
              .WithDependency<IDependency, IDependency>(dependencyMock.Object)
              .Build();
    
service.DoSomething();

dependencyMock.Verify(dependency => dependency.Do(), Times.Once);
......

#####1.5. AddingTo

Allows setting elements in a collection, one by one:

var customer = FluentBuilder<Customer>
              .New()
              .AddingTo(c => c.Addresses, new Address("1st Street"))
              .AddingTo(c => c.Addresses, new Address("2nd Street"))
              .Build();

Or:

var customer = FluentBuilder<Customer>
              .New()
              .AddingTo(c => c.Addresses, FluentBuilder<Address>.New().With(a => a.Street, "1st Street").Build())
              .AddingTo(c => c.Addresses, FluentBuilder<Address>.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:

IEnumerable<Address> addresses = FluentBuilder<Address>
                                .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':

Address address = FluentBuilder<Address>.New().With(a => a.Street, "1st Street");

####3. Extensions

#####3.1. Mock.Moq

#####3.2. Brazilian Version

fluentbuilder's People

Contributors

gimoteco avatar wellguimaraes avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.