Skip to content

Instantly share code, notes, and snippets.

@ssboisen ssboisen/Test.cs
Last active Dec 25, 2015

Embed
What would you like to do?
public enum TradeStatusEnum { Created, Approved }
public class Party
{
//ctor etc.
public string Name { get; protected set; }
}
public class Trade
{
//ctor etc.
public TradeStatusEnum TradeStatus { get; protected set; }
public DateTimeOffset TradeTime { get; protected set; }
public Party CounterParty {get; protected set; }
}
public class Tests
{
[Test]
public void TheTest()
{
var data = new Trade()
.SetWith(x => x.TradeStatus, TradeStatusEnum.Approved)
.UpdateWith(x => x.TradeTime, time => time.AddDays(3.5))
.SetWith(x => x.Party.Name, "Jon Doe");
//Do your test
}
}
public static class TestUtils
{
public static T SetWith<T, R>(this T instance, Expression<Func<T, R>> chainSelector, R value)
{
var selectorString = chainSelector.ToString().Substring(chainSelector.ToString().IndexOf('.') + 1);
Impromptu.InvokeSetChain(instance, selectorString, value);
return instance;
}
public static T UpdateWith<T, R>(this T instance, Expression<Func<T, R>> chainSelector, Func<R, R> valueTransformer)
{
var selectorString = chainSelector.ToString().Substring(chainSelector.ToString().IndexOf('.') + 1);
var value = Impromptu.InvokeGetChain(instance, selectorString);
Impromptu.InvokeSetChain(instance, selectorString, valueTransformer(value));
return instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.