+关注继续查看

# 有说错或不准确的地方欢迎留言指正

• Assert.AreEqual 与 Assert.AreNotEqual
• Assert.AreApproximatelyEqual 与 Assert.AreNotApproximatelyEqual
• Assert.IsTrue 与 Assert.IsFalse
• Assert.IsNull 与 Assert.IsNotNull
##### 检测是否违背指定的相等或不相等原则
    public void TestAssertEqual()
{
Assert.AreEqual("123", "456", "断言检测违背相等原则");

Assert.AreNotEqual("123", "123", "断言检测违背不相等原则");
}

##### 检测是否约等于或不等于是否成立
    public void TestAreApproximatelyEqual()
{
// 默认允许误差小于 0.00001f，这里我们制定误差为 0.01f
Assert.AreApproximatelyEqual(0.9f, 0.91f, 0.01f, "断言检测 约等于 不成立");

Assert.AreNotApproximatelyEqual(0.9f, 0.9000001f, 0.01f, "断言检测 不约等于 不成立");
}

##### 检测数值为True或False

public void TestIsTrue()
{
Assert.IsTrue(1 > 2, "违背结果为真的原则");

Assert.IsTrue(Testbool());

Assert.IsFalse(1 < 2, "违背结果为假的原则");
}

public bool Testbool()
{
return false;
}

##### 检测是否为Null
    public void TestIsNull()
{
Student student1 = new Student();

Assert.IsNull(student1,"检测结果值不为null");

student1 = null;

Assert.IsNotNull<Student>(student1,"检测结果为null");
}


#### 在断言中也可以自定义比较规则，代码如下

public class Student
{
public string name;
public int number;
public int age;
}

    public void TestCustomEqualityComparer()
{
Student S1 = new Student() { age = 1 };
Student S2 = new Student() { age = 2 };
Assert.AreEqual(S1, S2, "自定义比较结果为不相等", new CustomEqualityComparer<Student>((s1, s2) => { return s1.age == s2.age; }));
}

##### 比较的部分在于继承 IEqualityComparer 重新实现我们要比较的规则
public class CustomEqualityComparer<T> : IEqualityComparer<T>
{
public  Func<T,T, bool> EqualsComparer;

public CustomEqualityComparer(Func<T, T, bool> equalsComparer)
{
EqualsComparer = equalsComparer;
}
public bool Equals(T x, T y)
{
if ( EqualsComparer!=null)
{
return EqualsComparer(x, y);
}
else
{
return false;
}
}

public int GetHashCode(T obj)
{
return obj.ToString().GetHashCode();
}
}


### Assert.raiseExceptions = false;

    public void TestRaiseExceptions()
{
Assert.raiseExceptions = false;

Assert.IsTrue(1 > 2);

Assert.IsTrue(1 > 2, "自定义输出");

Debug.LogError(new string('*', 20));

Debug.Log("执行");
}


### Assert.raiseExceptions = true;

    public void TestRaiseExceptions()
{
Assert.raiseExceptions = true;

Assert.IsTrue(1 > 2);

Assert.IsTrue(1 > 2, "自定义输出");

Debug.LogError(new string('*', 20));

Debug.Log("执行");
}


# 打完收工~

【Debug 篇】FTP 无法连接目录或显示空白
FTP 无法连接目录或显示空白，报错提示 “服务器发回了不可路由的地址，使用服务器地址代替”。
1916 0
Docker入门系列之一：在一个Docker容器里运行指定的web应用
Docker入门系列之一：在一个Docker容器里运行指定的web应用
50 0
Changing the Way of Continuous Delivery with Docker (Part 2)
Docker has reformed the way of conducting continuous delivery. Docker allows you to package an application with all of its dependencies into a standar.
1841 0
Changing the Way of Continuous Delivery with Docker (Part 1)
The internet industry has been constantly changing market demands and products, forcing organizations to adapt by making constant deliveries and updates to their production environment.
1651 0
Unity3d 下websocket的使用

3168 0

1100 0

1400 0
+关注
su9257_海澜

98

0