Skip to content

Getting Started

igor-toporet edited this page Jun 11, 2012 · 5 revisions

Hello, World!

Consider following class has been declared

public class Greeting
{
    public string Message { get; set; }
}

We can write following test (just for demo purpose)

[Fact]
public void Example_01_HelloWorld()
{
    var greeting = TestData.Create<Greeting>(HelloWorld);

    Assert.Equal("Hello, World!", greeting.Message);
}

Where "HelloWorld" is just regular method which customizes Greeting instance immediately upon creation.

private void HelloWorld(Greeting greeting)
{
    greeting.Message = "Hello, World!";
}

So TestData not only creates instance of desired type but also applies to it any customization at your will.

Let's greet friends

So here is a little bit more complicated example where we are going to apply few customizations at once. Notice the wording, its expressiveness is limited by your imagination only.

[Fact]
public void Example_02_GreetYourFriends()
{
    var greeting = TestData.Create(Greeting("Hi"), To("Alice"), And("Bob"));

    Assert.Equal("Hi, Alice and Bob!", greeting.Message);
}

Where methods Greeting(), To() and And() are declared like this

private Action<Greeting> Greeting(string something)
{
    return greeting => greeting.Message = something;
}

private Action<Greeting> To(string friend)
{
    return greeting => greeting.Message += ", " + friend;
}

private Action<Greeting> And(string friend)
{
    return greeting => greeting.Message += " and " + friend + "!";
}

It's time for permanent customization demo

Let's imagine we declared following interfaces to be implemented by all persistent types in our application

public interface IIdentifiable<TKey> where TKey : struct
{
    TKey Id { get; set; }
}

public interface IVersionable
{
    int Version { get; set; }
}

And here is one of our domain classes implements both IIdentifiable and IVersionable

public class Resource : IIdentifiable<int>, IVersionable
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Version { get; set; }
}

And let's assume while testing our classes we'd prefer to see in debug some meaningful values of Id and Version properties, rather than just defaulted to zero ones. In this case we can declare somewhere in fixture setup following permanent customizations

public PermanentCustomizationFixture()
{
    TestData.SetPermanentCustomizations<IVersionable>(v => v.Version = RandomInRange(1, 9));
    TestData.SetPermanentCustomizations<IIdentifiable<int>>(i => i.Id = RandomInRange(100, 999));
}

Thus every time we create instance of any type which implements IIdentifiable or IVersionable it gets corresponding properties Id and Version set to some random values in specified ranges

[Fact]
public void Example_03_PermanentCustomizationsDemo()
{
    var resource = TestData.Create<Resource>();

    Assert.True(100 <= resource.Id && resource.Id <= 999);
    Assert.True(1 <= resource.Version && resource.Version <= 9);
}
Clone this wiki locally