测试Foreach 函数(Testing a Foreach function)

编程入门 行业动态 更新时间:2024-10-24 22:19:47
测试Foreach 函数(Testing a Foreach function)

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); }

更多推荐

本文发布于:2023-08-05 07:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428794.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   测试   function   Foreach   Testing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!