- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个用列表构建的深树结构,包含列表和 float 。我想对这样的结构进行断言,而不能对 float 使用增量。我的问题是,使用失败断言的输出是不够的,因为需要一到两位额外的小数。我必须猜测这些小数点才能继续。
using System.Collections;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestAssertLab
{
public class List : ArrayList {
public List(params object[] list) {
AddRange(list);
}
public override bool Equals(object other) {
List l = other as List;
if (l != null) {
if (Count != l.Count) return false;
return !l.Cast<object>().Where((t, i) => !this[i].Equals(t)).Any();
}
return false;
}
public override string ToString() {
string s = this.Cast<object>().Aggregate("", (current, item) => current + (item + ","));
return "[" + s.TrimEnd(',') + "]";
}
}
[TestClass]
public class AssertLab
{
public List z(params object[] l) {
return new List(l);
}
[TestMethod]
public void TestFails() {
List expected = z(0.1428571f, z(101, 102));
List actual = z(1/7.0f, z(101, 102));
Assert.AreEqual(expected, actual);
// output: Assert.AreEqual failed. Expected:<[0.1428571,[101,102]]>. Actual:<[0.1428571,[101,102]]>.
}
[TestMethod]
public void TestOK()
{
List expected = z(0.142857143f, z(30101, 30102));
List actual = z(1 / 7.0f, z(30101, 30102));
Assert.AreEqual(expected, actual);
}
}
}
最佳答案
您可以将它们转换为十进制:
(Decimal) 0.1428571f == (Decimal)(1 / 7.0f)
关于c# - Assert.AreEqual for float without delta,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9950644/
考虑这个结构: public struct MyNumber { private readonly int _value; public MyNumber(int myNumber)
我正在为我正在处理的一个简单的控制台游戏项目运行以下测试,该项目因 Assert.AreEqual 失败而失败。 [TestMethod] public void TestMethod1()
我有一个我无法解释的非常奇怪的行为。 我有以下类(class): public class Project { public virtual int Id { get; set; }
我有以下单元测试: string MtrlCode = "0"; Assessment target = new Assessment(MtrlCode); List EdgeCaseSymbolCo
我正在尝试使用 比较两个列表 CollectionAssert.AreEqual(ListExpected, ListActual); 但是我得到了一个异常(exception) Expected
我的父组件处理对服务器的请求并为其子组件提供来自响应的数据。在这种情况下,一个子组件(FilterSidebar:包含过滤器组件)应该只呈现一次,因为过滤器将从初始请求的数据创建。 这就是为什么我要使
我对从集合中获取对象的方法进行了单元测试。这一直失败,我不明白为什么,所以我在下面创建了一个非常简单的测试来创建 2 个供应商对象并测试它们是否相等,看看我是否可以在我的代码测试中发现问题。但是这个测
java.lang.StackOverflowError at kotlin.jvm.internal.Intrinsics.areEqual(Intrinsics.java:164)
假设我有一个 data class Eq(x: Int?) 这将生成一个看起来像这样的 equals 方法 public boolean equals(Object other){ if(th
我是 Java 初学者。我想在下面的类中实现一些基本功能。 /*Demonstrate the different data types in java*/ import java.io.*; imp
我正在为代码生成器编写单元测试,我的大部分测试将跨越多行的输出与一些黄金输出进行比较。目前,我正在将输出与 Assert.AreEqual(string, string) 进行比较,后者显示第一个差异
举个例子: string x = null; var y = String.Empty; Assert.AreEqual(x, y); // test fails! Asser
我正在进行一些单元测试,但不确定为什么这个特定测试会失败。 测试是为了断言自定义 View 引擎在正确的位置查找 View 。 在我的自定义 View 引擎中是这样的: AreaMasterLocat
VS UnitTest 项目中断言的标准行为是仅告诉我测试在特定行上失败。 但是有时候,如果我可以在这样的断言失败时中断,那会很方便。因为仅在该行设置断点也会导致测试用例不失败。如果我可以在断言失败时
我对这一切都很陌生,所以任何帮助都将不胜感激。 我已经从 url 方法创建了一个下载图像,我需要对其进行测试。 我已尝试填写测试参数,但我不确定要输入什么。 你能告诉我要去哪里吗, 谢谢。亚伦 [Te
示例: Assert.AreEqual(**null**, Program.nDaysMonth(5, -10), "Error nDaysMonth, Month may -10."); 我希望有一
我有一个类 MyCustomClass: public MyCustomClass { public MyCustomClass() { MyObject = new
我正在编写一些单元测试,但以下断言失败了: Assert.AreEqual(expected.Episode, actual.Episode); 如果我改为调用它,它会成功: Assert.IsTru
更新 3: This page告诉我如何用三行来做。任何人都知道如何在一行中完成它? for (int i = 0; i < 5; i++) { my_value_to_verify = get
我创建了一个自定义结构来表示金额。它基本上是 decimal 的包装器。它有一个隐式转换运算符,可以将其转换回 decimal。 在我的单元测试中,我断言金额等于原始十进制值,但测试失败。 [Test
我是一名优秀的程序员,十分优秀!