Hello Guys我必须测试下一个函数但是因为我刚开始用C#编程我被卡住了,我整天都在阅读有关IEnumerables和Enumerators的内容,但我还不能对这个函数进行单元测试。
这是功能
public static void ForEach<TSource>([NotNull] this IEnumerable<TSource> source, [NotNull] Action<TSource> action) { Contract.Requires(source != null); Contract.Requires(action != null); foreach (var item in source) { action(item); } }我这样调用函数但它不起作用..
[TestMethod] public void ForEach_Test() { //PREPARE List<string> listToBeTested = new List<string>(); listToBeTested.Add("Any string"); listToBeTested.Add("Any string"); listToBeTested.Add("Any string"); //EXECUTE List<string> listformatted = new List<string>(); listToBeTested.ForEach(listformatted.Add("any string")); //ASSERT Assert.AreEqual(listToBeTested, listformatted); }Hello Guys I have to test the next function but since I just start with programming in C# I am stuck, I was reading the whole day about IEnumerables and Enumerators but I can't make the unit test for this function yet.
here is the function
public static void ForEach<TSource>([NotNull] this IEnumerable<TSource> source, [NotNull] Action<TSource> action) { Contract.Requires(source != null); Contract.Requires(action != null); foreach (var item in source) { action(item); } }I call the function like this but it's not working..
[TestMethod] public void ForEach_Test() { //PREPARE List<string> listToBeTested = new List<string>(); listToBeTested.Add("Any string"); listToBeTested.Add("Any string"); listToBeTested.Add("Any string"); //EXECUTE List<string> listformatted = new List<string>(); listToBeTested.ForEach(listformatted.Add("any string")); //ASSERT Assert.AreEqual(listToBeTested, listformatted); }最满意答案
我不确定你要测试的是什么,但我可以用猜测来帮助你解决语法问题。
[TestMethod] public void ForEach_Test() { //PREPARE List<string> listToBeTested = new List<string>(); listToBeTested.Add("Any string"); listToBeTested.Add("Any string"); listToBeTested.Add("Any string"); //EXECUTE List<string> listformatted = new List<string>(); listToBeTested.ForEach(x => listformatted.Add(x)); //ASSERT Assert.AreEqual(listToBeTested, listformatted); }现在,listToBeTested是ForEach中的source参数。 对于listToBeTested中的每个项目,调用的操作将是我们传递的lambda。 变量x表示lambda中的项,因此执行的内容将类似于:
foreach (var item in listToBeTested) { listformatted.Add(item); }I am not exactly sure what you are trying to test, but I can take a guess help you out with the syntax.
[TestMethod] public void ForEach_Test() { //PREPARE List<string> listToBeTested = new List<string>(); listToBeTested.Add("Any string"); listToBeTested.Add("Any string"); listToBeTested.Add("Any string"); //EXECUTE List<string> listformatted = new List<string>(); listToBeTested.ForEach(x => listformatted.Add(x)); //ASSERT Assert.AreEqual(listToBeTested, listformatted); }Now, listToBeTested is the source parameter in your ForEach. For every item in the listToBeTested, the action called will be the lambda we are passing. The variable x represents the item in the lambda, so what is executed will be something like:
foreach (var item in listToBeTested) { listformatted.Add(item); }更多推荐
发布评论