Giter Club home page Giter Club logo

autofixture.extensions's Introduction

AutoFixture Extensions

Build Status Build status

Methods

With<T, TProperty, TValue>()

  • T - entity type
  • TProperty - target property type
  • TValue - source value type

Allows building entity using regular IFixture.With() method, but with different types of source value and destination property. A converter between specified types must be registered. There are some predefined converters, but if there is no appropriate one, it is possible to register a custom converter using Extend() method.

public class Foo
{
  public string Value { get; set }
}

int value = ...;

var foo = new Fixture()
  .Build<Foo>()
  .With(x => x.Value, value)
  .Create();

Json<T, TProperty, TValue>()

Converts parameter of type TValue to a property of type TProperty using JSON serialization. It uses With() extension method, and since JSON is basically a string, corresponding value converter between string type and TProperty type has to be registered. Although, if TProperty is a string type, no extra converter is needed.

public class Foo { }
public class Bar
{
  public string Json { get; set; }
}

var foo = new Foo();

var bar = new Fixture()
  .Build<Bar>()
  .Json(x => x.Json, foo)
  .Create();

Extend(this IFixture fixture, Type source, Type target, IValueConverter converter)

Registers converter between a source and target types.

public class Foo { }
public class Bar { }

public class Baz
{
  public Bar Bar { get; set; }
}

var foo = new Foo();

var baz = new Fixture()
  .Extend(typeof(Foo), typeof(Bar), new FooBarConverter())
  .Build<Baz>()
  .With(x => x.Bar, foo)
  .Create();

Classes

ValueConverter

It is a base type for custom value converters. Use Extend() method to register custom value converter.

public class Foo { }
public class Bar { }

public class FooBarConverter : ValueConverter<Foo, Bar>
{
  protected override Bar Convert(Foo value)
  {
    var bar = new Bar();
    
    // initialization logic
    
    return bar;
  }
}

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.