从生产代码中删除断言的典型论据是性能。这对我来说没有意义。是的,从性能关键的 5% 左右的代码中剥离一些断言可能是一种有用的优化。然而,对于另外 95% 的人来说,它们可能没有可测量的效果,并且断言只会增加这样的可能性:如果您的代码有错误,它会以一种易于诊断的方式快速失败。
我的大部分编程都是在 D 中完成的,它的enforce()功能基本上可以完成相同的assert()功能,只是它保留在发布版本中。我通常发现自己enforce()大部分时间都在使用,而且assert()只在少数enforce()太贵的地方使用。
除了性能之外,还有其他原因从发布版本中删除断言吗?如果不是,为什么语言不让断言的默认行为即使在发布版本中也始终执行,并提供第二个更冗长且更难记住的函数,类似的东西expensiveAssert()从发布版本中删除并建议仅在代码中性能关键的部分?
无法捕获断言。这很好,因为我不想将某些错误包含在 try/catch 中,至少不在开发服务器上。但断言似乎非常危险。如果他们投入生产,它可以使用 msgbox 挂起 ASP.NET 服务器。
//Don't want this on prod even if debug=true is in the web.config
#if DEBUG
//A future client programmer can wrap this in a try{}catch{}
if (!EverythingIsOkay)
throw new InvalidOperationException("Dagnabbit, programming error");
//This stops the but has less information that an
// Exception and hangs the server if this accidentally
// runs on production
System.Diagnostics.Debug.Assert(!EverythingIsOkay);
#endif
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法可以向开发人员传达违反不可侵犯条件的情况,而不会冒 IIS 挂起的风险?
更新:阅读第一个回复后,我想答案取决于一种万无一失的方法来检测代码何时在开发环境中运行以及何时在生产服务器上运行,或者弄清楚如何抛出无法捕获的异常并被忽略。
我正在为我编写的一些 .net 代码编写单元测试。
我熟悉这样编写代码:
int expected = 10;
int actual = multiplyByTwo(5);
Assert.AreEqual(expected, actual);
Run Code Online (Sandbox Code Playgroud)
在参数是整数的情况下,很清楚期望代码做什么。
当传入的参数是对象时,代码会做什么?
如果我编写了一个名为 的自定义类MyClass,那么如何控制Assert.AreEqual类型对象的通过和失败时间MyClass?
我必须验证包含大约 40 个必填字段的请求。
我想通过避免经典来做到这一点if (field1 == null) throw new XXException("msg");
例如我有以下代码:
if (caller == null)
{
// Caller cannot be empty
throw new CallerErrorException(new CallerError("", "incorrect caller"));
}
if (person == null)
{
// Person cannot be empty
throw new PersonErrorException(new CustomerError("", "incorrect customer"));
}
if (module == null)
{
// Module cannot be empty
throw new ModuleErrorException(new ModuleError("", "module must be X"));
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,根据哪个字段为空,会抛出特定的自定义异常和自定义消息。
所以,我想要这样的东西:
assertNotEquals(call, null, new CallerErrorException(new CallerError("", "incorrect caller")));
assertNotEquals(person, null, new PersonErrorException(new CustomerError("", "incorrect …Run Code Online (Sandbox Code Playgroud) 有没有办法将 Python 对象与深度复制生成的对象进行比较?
例如:
import copy
original_object = SomeObject()
cloned_object = copy.deepcopy(original_object)
assertDeepCopy(original_object, cloned_object)
Run Code Online (Sandbox Code Playgroud) 使用节点的断言模块如何测试错误消息?
throw new Error('Email is required!');
Run Code Online (Sandbox Code Playgroud)
我使用assert.throws来检查是否抛出错误:
assert.throws(myFunction, Error);
Run Code Online (Sandbox Code Playgroud)
但这并不提供检查消息的能力。
我想使用断言测试 gcd 函数,但我不知道如何捕获异常(并防止程序崩溃)。
int gcd(int a, int b) {
if(a<0 || b<0) {
throw "Illegal argument";
}
if(a==0 || b==0)
return a+b;
while(a!=b) {
if(a>b) {
a = a - b;
}
else {
b = b - a;
}
}
return a;
Run Code Online (Sandbox Code Playgroud)
}
void test_gcd() {
assert(gcd(16,24) == 8);
assert(gcd(0, 19) == 19);
try {
gcd(5, -15);
assert(false);
} catch (char* s) {
assert(true);
cout << "Illegal";
}
Run Code Online (Sandbox Code Playgroud)
}
我想获取我正在为后续测试测试的函数的返回值。函数定义如下:
func apple(banana: Banana) throws -> Cherry { ... }
我可以测试它应该在什么时候抛出:
XCTAssertThrowsError(try apple(banana: badBanana), "Didn't throw")
我可以测试它在不应该抛出时不会抛出:
XCTAssertNoThrow(try apple(banana: goodBanana), "Did throw")
我希望这样做:
XCTAssertNoThrow(let cherry = try apple(banana: goodBanana), "Did throw")
然后检查cherry是我所期望的,但我收到以下错误:Consecutive statements on a line must be separated by ';'...
如何Cherry从XCTAssertNoThrow测试中获取返回值(在本例中为类型对象)?或者有没有更好的方法我错过了?
非常感谢
我正在创建几个单元测试,我想验证是否使用我期望的属性调用方法。
因此,鉴于这个非常简单的系统:
public class Employee
{
public bool IsEmployed { get; set; }
}
public class DataStore
{
public void UpdateEmployee(Employee obj)
{
// Save in DB
}
}
public interface IDataStore
{
void UpdateEmployee(Employee employee);
}
public Employee FireEmployee(IDataStore dataStore, Employee employee)
{
employee.IsEmployed = false;
dataStore.UpdateEmployee(employee);
return employee;
}
Run Code Online (Sandbox Code Playgroud)
我想验证该DataStore.UpdateEmployee()方法是否在Employee.IsEmployed属性设置为 false时被调用。所以这里有两个测试用例,我认为它们应该完成同样的事情。
[Test]
public void TestViaVerify()
{
//Arrange
Mock<IDataStore> dataStore = new Mock<IDataStore>();
var robert = new Employee { IsEmployed = true };
//Act
FireEmployee(dataStore.Object, …Run Code Online (Sandbox Code Playgroud) 我正在为语言标记器编写一些测试,并将标记器生成的标记化的 JSON 序列化版本与已知良好标记化的序列化进行比较。所以我有一些这样的测试:
#[test]
fn test_tokenize() {
let actual_token_json = /* string */;
let expected_token_json = /* string */;
assert_eq!(actual_token_json, expected_token_json);
}
Run Code Online (Sandbox Code Playgroud)
但是,当这些测试失败时,cargo test只是说 JSON 字符串不相等并将它们都打印出来,这不是很有用。由于 JSON 字符串都打印得很漂亮,如果cargo test打印出两个字符串的行差异会更有帮助。有什么办法可以做到这一点吗?
更新:有人编辑了这篇文章的标题,但删除了一条重要信息:当两个字符串不相等时,我正在寻找行差异。我在这里看到的答案之一是针对更一般的问题,即在两个值不相等时获取两个值的调试表示的行和字符差异。这要么超过我需要的,要么不是我需要的。例如,两个字符串的调试表示的差异不会给我一个行差异,而是一个大的、复杂的字符差异(因为这些行在调试表示中显示为转义的“\n”标记)。