Last active
December 25, 2015 06:39
-
-
Save ssboisen/6933906 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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