简体   繁体   English

不知道我在单元测试中做错了什么

[英]Don't know what I'm doing wrong in my Unit Test

I have a Unit Test for my LoginSystem, and it's failing and I have no idea why, In my opinion I think my code is correct, it seemed to be working for the past couple of weeks and I only found this error through Unit Testing. 我为LoginSystem进行了单元测试,但失败了,我不知道为什么,我认为我的代码是正确的,它似乎在过去的几周内都可以正常工作,并且我仅通过单元测试发现了此错误。

public bool matchCheck(string Username, string Password)
        {
            //create a new bool and set it to false.
            bool returnvar = false;

            //for each basemember in the list of members.
            foreach (BaseMember bm in hillracing.Members)
            {
                //if the username is equal to a list item username AND the password is also equal.
                if (Username == bm.userName && Password == bm.passWord)
                {
                    //they exist within the same object, so return true.
                    returnvar = true;
                }
                else
                {
                    throw new Exception(" Did not match!");
                }
            }

            //return this bool, true or false.
            return returnvar;
        }

Can someone tell me why my exception is always thrown? 有人可以告诉我为什么总是抛出我的异常吗? thanks. 谢谢。

You're comparing ONE username and password to an entire collection of usernames and passwords. 您正在将一个用户名和密码与整个用户名和密码集合进行比较。 By definition, only one item in the entire collection should evaluate to true and the rest should evaluate as false . 根据定义,整个集合中只有一项应评估为true ,其余应评估为false In your case, you're throwing an exception the very first time one evaluates to false . 在您的情况下,您第一次将异常评估为false时引发了异常。

On a side note, comments like this one: 附带说明一下,这样的注释:

//create a new bool and set it to false. //创建一个新的布尔并将其设置为false。

bool returnvar = false; bool returnvar = false;

Should be avoided since the code talks for itself. 应避免使用,因为代码本身就是自言自语。 Useless comments dont add any value to the code but rather have a negative effect. 无用的注释不会给代码增加任何价值,但是会带来负面影响。

If you are looking to improve the routine, then consider returning true when a match is found, if it gets to the end of the loop, return false or throw an exception. 如果要改进例程,请考虑在找到匹配项时返回true,如果到达循环末尾,则返回false或引发异常。 My personal preference is to avoid throwing exceptions unless it is actually an exception. 我个人的喜好是避免引发异常,除非它实际上是异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道我的 C# 代码做错了什么 - I don't know what I'm doing wrong in my C# code 我收到一个错误:使用未分配的局部变量“艺术家”,不知道我在做什么错? - I get an error: use of unassigned local variable 'artist', don't know what i'm doing wrong? 我正在验证 C# 中的表格,但我不知道我做错了什么。 请帮我解决这个问题 - I'm validating a form in C# but I don't know what I am doing wrong. Please help me solve this 有时我不知道某些方法属于哪些类。 我究竟做错了什么? - Sometimes I don't know which classes certain methods belong to. What am I doing wrong? 在Visual Studio 2010中进行单元测试(c#)。我做错了什么? 测试总是失败 - Making a unit test (c#) in Visual Studio 2010. What I'm doing wrong? The test fails always 收到错误,Collection 被修改; 枚举操作可能无法执行,但我不知道我做错了什么,在哪里? - Receiving an error, Collection was modified; enumeration operation may not execute, but I don't know what I am doing wrong and where? 无法在其他课程中引用公共对象,请查看我的资料并让我知道我在做错什么 - Cannot reference public object in another class, please look at my source and let me know what I'm doing wrong 我的统一项目正在冻结这个脚本我不知道我做错了什么 - My unity project is Freezing with this script i don't know what have i done wrong 我正在统一使用Raycast,当我单击它时我想获取对象的位置,但是我不工作并且我不知道我在做什么错 - I am using Raycast in unity and i want to get the position of an object when i click on it but i does not work and i don't know what i am doing wrong 我正在使用 Alpha Vantage API 来尝试获取每日库存信息。 我对使用 API 真的很陌生,不知道我做错了什么 - I am using Alpha Vantage API to try and pull daily stock info. I am really new to using APIs and don't know what I am doing wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM