【问题标题】:How can I override jasmine's buildExpectationResult in order to modify message() function?如何覆盖 jasmine 的 buildExpectationResult 以修改 message() 函数?
【发布时间】:2015-11-11 14:48:04
【问题描述】:

我使用量角器进行 e2e 测试,并使用 jasmine2 作为框架。我正在使用带有屏幕截图的 html 记者插件 (html-report for protractor)。

在这些报告中,将显示所有失败/通过预期的列表。当 expect 失败时,我会收到关于期望的描述性消息。然而,当 expect 过去时,我只看到这个词:Passed。 其背后的原因是,当 expect 过去时,jasmine 会覆盖消息。

这是在以下文件中完成的:

 node_modules/protractor/node_modules/jasmine/node_modules/jasmine-core/lib/jasmine-core/jasmine.js
getJasmineRequireObj().buildExpectationResult = function () {
    function buildExpectationResult(options) {
        var messageFormatter = options.messageFormatter || function () {
                },
            stackFormatter = options.stackFormatter || function () {
                };

        var result = {
            matcherName: options.matcherName,
            message: message(),
            stack: stack(),
            passed: options.passed
        };

        if (!result.passed) {
            result.expected = options.expected;
            result.actual = options.actual;
        }

        return result;

        function message() {
            if (options.passed) {
                // Here is the message overriden
                return 'Passed.';
            } else if (options.message) {
                return options.message;
            } else if (options.error) {
                return messageFormatter(options.error);
            }
            return '';
        }

        function stack() {
            if (options.passed) {
                return '';
            }

            var error = options.error;
            if (!error) {
                try {
                    throw new Error(message());
                } catch (e) {
                    error = e;
                }
            }
            return stackFormatter(error);
        }
    }

    return buildExpectationResult;
};

我想要的是在我的量角器 protractor.conf.js 文件中覆盖这个函数。并将其替换为具有所需行为的一个。

我尝试这样做但未成功:

onPrepare: function () {
    jasmine.buildExpectationResult = function () {
        function buildExpectationResult(options) {
            var messageFormatter = options.messageFormatter || function () {
                    },
                stackFormatter = options.stackFormatter || function () {
                    };

            return {
                matcherName: options.matcherName,
                expected: options.expected,
                actual: options.actual,
                message: message(),
                stack: stack(),
                passed: options.passed
            };

            function message() {
                if (options.message) {
                    return options.message;
                } else if (options.error) {
                    return messageFormatter(options.error);
                }
                return "";
            }

            function stack() {
                if (options.passed) {
                    return "";
                }

                var error = options.error;
                if (!error) {
                    try {
                        throw new Error(message());
                    } catch (e) {
                        error = e;
                    }
                }
                return stackFormatter(error);
            }
        }

        return buildExpectationResult;
    };
} 

那么我的问题是:覆盖 jasmine 方法的正确方法是什么?

【问题讨论】:

    标签: jasmine protractor


    【解决方案1】:

    由于我们使用 gulp 任务来运行量角器测试,因此我们将 lib(如 jasmine lib)覆盖为具有自定义副本的 gulp 任务之一。我们将其作为安装或每次测试执行的一部分进行。

    除非我们创建另一个 npm 模块,否则我没有找到任何好方法来覆盖它。

    【讨论】:

    • 你找到其他方法了吗?
    【解决方案2】:

    我有同样的问题,我不确定我的解决方案是否

    onPrepare: function () {
    // ...
    jasmine.Spec.prototype.addExpectationResult = function(passed, data, isError) {
      var buildExpectationResult = function(options) {
          var messageFormatter = options.messageFormatter || function() {},
            stackFormatter = options.stackFormatter || function() {};
      
          var result = {
            matcherName: options.matcherName,
            message: message(),
            stack: stack(),
            passed: options.passed
          };
      
          if(!result.passed) {
            result.expected = options.expected;
            result.actual = options.actual;
          }
      
          return result;
      
          function message() {
            if (options.passed) {
              return options.message ? options.message : 'Passed';
            } else if (options.message) {
              return options.message;
            } else if (options.error) {
              return messageFormatter(options.error);
            }
            return '';
          }
      
          function stack() {
            if (options.passed) {
              return '';
            }
      
            var error = options.error;
            if (!error) {
              try {
                throw new Error(message());
              } catch (e) {
                error = e;
              }
            }
            return stackFormatter(error);
          }
        }
    
      var exceptionFormatter = jasmine.ExceptionFormatter;
    
      var expectationResultFactory = function(attrs) {
        attrs.messageFormatter = exceptionFormatter.message;
        attrs.stackFormatter = exceptionFormatter.stack;
    
        return buildExpectationResult(attrs);
      }
    
      var expectationResult = expectationResultFactory(data);
    
      if (passed) {
        this.result.passedExpectations.push(expectationResult);
      } else {
        this.result.failedExpectations.push(expectationResult);
    
        if (this.throwOnExpectationFailure && !isError) {
          throw new j$.errors.ExpectationFailed();
        }
      }
    };
    
    // ...
    }

    【讨论】:

    猜你喜欢
    • 2020-04-04
    • 2019-06-19
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多