测试方法失败(VS 中的单元测试)
我正在建立一个进行单元测试的实用实验室。下面是我正在测试的应用程序(我正在测试所有方法和构造函数)。我的所有测试都完全接受,即测试一种名为“isScalene()”的方法,该方法检查三角形是否是不等边三角形(所有边都是唯一的)。
您会在底部找到失败的测试方法。当我将“等边”更改为 True 并将“scalene”更改为 False 时,它就通过了。应用程序中出了点问题,但我就是不知道它是什么(可能在“uniqueSides() 中)。
如果有人可以帮助我,我将不胜感激!
public class Triangle {
double[] sides;
public Triangle(double a, double b, double c) {
if ((a <= 0) || (b <= 0) || (c <= 0)){
throw new ArgumentOutOfRangeException("Sidorna måste vara större än 0.");
}
sides = new double[] { a, b, c };
}
private int uniqueSides() {
return sides.Distinct<double>().Count();
}
public bool isScalene() {
if (uniqueSides() == 1){
return true;
}
else{
return false;
}
}
public bool isEquilateral() {
if (uniqueSides() == 3){
return true;
}
else{
return false;
}
}
public bool isIsosceles() {
if (uniqueSides() == 2){
return true;
}
else{
return false;
}
}
}
...
[TestMethod()]
public void isScalene3Test3()
{
Triangle triangle = new Triangle(25, 250, 2000);
var isosceles = triangle.isIsosceles();
var equalateral = triangle.isEquilateral();
var scalene = triangle.isScalene();
Assert.IsFalse(isosceles, "Test Isosceles");
Assert.IsFalse(equalateral, "Test Equalateral");
Assert.IsTrue(scalene, "Test Scalene");
}
I'm working on a laboratory practical with unit-testing. Below is the application that i'm testing (i'm testing all the methods and the constructor). All my tests are complete accept for one, that is testing a method called "isScalene()", that checks wether the triangle is a scalene (all sides are unique) or not.
You find the test method that fails at the bottom. When I change "equalateral" to True and "scalene" to False, it passes. Something is wrong in the application, but I just can't figure out what it is (probably in "uniqueSides() though).
I would be grateful if someone could help me out!
public class Triangle {
double[] sides;
public Triangle(double a, double b, double c) {
if ((a <= 0) || (b <= 0) || (c <= 0)){
throw new ArgumentOutOfRangeException("Sidorna måste vara större än 0.");
}
sides = new double[] { a, b, c };
}
private int uniqueSides() {
return sides.Distinct<double>().Count();
}
public bool isScalene() {
if (uniqueSides() == 1){
return true;
}
else{
return false;
}
}
public bool isEquilateral() {
if (uniqueSides() == 3){
return true;
}
else{
return false;
}
}
public bool isIsosceles() {
if (uniqueSides() == 2){
return true;
}
else{
return false;
}
}
}
...
[TestMethod()]
public void isScalene3Test3()
{
Triangle triangle = new Triangle(25, 250, 2000);
var isosceles = triangle.isIsosceles();
var equalateral = triangle.isEquilateral();
var scalene = triangle.isScalene();
Assert.IsFalse(isosceles, "Test Isosceles");
Assert.IsFalse(equalateral, "Test Equalateral");
Assert.IsTrue(scalene, "Test Scalene");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
isEquiside 应使用 1 个独特的边,isScalene 应使用 3 个。
isEquilateral should use 1 unique side, and isScalene should use 3.
您不想检查是否
(uniqueSides() == 3)
而不是 1 吗?不管怎样,调试代码可以帮助你自己很快地发现这一点。
Don't you want to check if
(uniqueSides() == 3)
rather than 1?Anyway, debugging the code would have helped you find this out very quickly for yourself.