英文:
Can I get a return value from an expect() statement in Jasmine?
问题
I have some test automation code written in Jasmine to test a web app. It has many statements of the form: expect(a).toBe(b)
or expect(a).toEqual(b)
.
I am now working on updating my code to store results in a database. I have been given the direction that we don't care about storing results for tests which passed, and to only store failures.
My first attempt at this involves constructions like the following:
expect(a).toBe(b);
let result = (a == b) ? "pass" : "fail";
if(result == "fail") {
// write to database
}
However, there are some cases in which a
and b
are objects or arrays, rather than primitive types. In this case, I think I can use expect(a).toEqual(b)
to do a deep comparison, but I can't use the comparison of a == b
to decide whether this is a failure that should be in the database.
Is there a way for me to get a return value from the expect()
statement, or otherwise inspect whether the test passed or failed? This would both save me the trouble of writing my own comparator for a
and b
, and ensure that I am writing to the database only on a test case failure.
英文:
I have some test automation code written in Jasmine to test a web app. It has many statements of the form: expect(a).toBe(b)
or expect(a).toEqual(b)
.
I am now working on updating my code to store results in a database. I have been given the direction that we don't care about storing results for tests which passed, and to only store failures.
My first attempt at this involves constructions like the following:
expect(a).toBe(b);
let result = (a == b) ? "pass" : "fail";
if(result == "fail") {
// write to database
}
However, there are some cases in which a
and b
are objects or arrays, rather than primitive types. In this case, I think I can use expect(a).toEqual(b)
to do a deep comparison, but I can't use the comparison of a == b
to decide whether this is a failure that should be in the database.
Is there a way for me to get a return value from the expect()
statement, or otherwise inspect whether the test passed or failed? This would both save me the trouble of writing my own comparator for a
and b
, and ensure that I am writing to the database only on a test case failure.
答案1
得分: 2
I can provide a Chinese translation for the code-related content you provided:
你能够实现你自己的自定义报告器吗?
基于上面的示例,你可以覆盖specDone
函数以记录到数据库或其他任何地方。
const myReporter = {
specDone: function(result) {
console.log('用例: ' + result.description + ' 状态为 ' + result.status);
for (const expectation of result.failedExpectations) {
// 记录到数据库
}
},
};
将报告器注册到Jasmine:
jasmine.getEnv().addReporter(myReporter);
英文:
Could you implement your own Custom Reporter?
Building off the example above, you could override the specDone
function to log to a database or anything else.
const myReporter = {
specDone: function(result) {
console.log('Spec: ' + result.description + ' was ' + result.status);
for (const expectation of result.failedExpectations) {
// log to database
}
},
};
Register the reporter with jasmine
jasmine.getEnv().addReporter(myReporter);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论