在AngularJS的ngScenario测试中, 我们有时需要对返回的值做一些特殊的处理,然后再进行比较。

例如我们验证select的值为abcd

expect(element("select").text()).toBe("abcd")

但用jquery发现这个select的值为

\n   abc   \n

前后会多出一些空白字符。我们要做的是先对其进行trim操作,然后与abcd对比即可。

expect(_.str.trim(element("select").text())).toBe("abcd")

注意_.str.trim是Underscore.String提供的。

但这样是行不通的,_.str.trim函数接收到的是一个promise对象,所以里面没有text的值。

要正确的获得值,需要做一些改进。在这里有一些讨论,可以参考:

var textPromise = element('select').query(function (nameElement, done) {
    var text = _.str.trim(nameElement.text()); // Can finally access this guy!

    // The first param null indicates a nominal execution, the second param is a return of sorts
    done(null, text);
  });

  // Passes
  expect(textPromise).toBe('abcd')

这样,通过query这个函数,即可获得真正的text的值了。

这里还有一些别的例子,请参考原文。