你可以从Jasmine的`expect()`语句中获得返回值吗?

huangapple go评论65阅读模式
英文:

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);

huangapple
  • 本文由 发表于 2023年4月11日 04:18:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定