c# - Unit Testing Action<T>? -
i using xunit , moq , trying test this:
public static class ienumerableextensions { public static void foreach<t>(this ienumerable<t> list, action<t> action) { foreach (var item in list) action(item); } }
i have tried this, no deal:
[fact] public void foreach() { var list = new list<string>() { "string1", "string2" }; list.foreach(x => { x += "-passed"; }); foreach (var item in list) item.should().endwith("-passed"); }
how go testing this?
in current test, modifying local x
variable in action. create new list:
var list = new list<string>() { "string1", "string2" }; var dest = new list<string>(); list.foreach(s => { dest.add(x + "-passed"); }); foreach (var item in dest) { item.should().endwith("-passed"); }
you might want maintain count of how called:
int called = 0; list.foreach(s => { dest.add(x + "-passed"); called++; }); assert.areequal(called, list.count);
Comments
Post a Comment