Mocha / Chai测试linkTo函数返回AssertionError

时间:2015-03-30 11:02:35

标签: javascript function testing mocha chai

我试图找到&更正 challenge.js 中的Javascript代码,以便通过Mocha& Chai按照 spec.js 中的规定进行测试....当我在终端中运行命令$ mocha spec.js时,我尝试的任何东西都会让它通过测试....提供更多信息,无论我对 challenge.js 做出任何更改,都会在终端中不断返回生成的错误消息。

Challenge.js

module.exports.linkTo = function(text, address) {   
  return "<a href='" + text + "'>" + address + "</a>"
};

Spec.js

var expect = require("chai").expect;
var challenge = require("./challenge.js");

describe("linkTo", function() {
  it("should be defined", function() {
    expect(challenge.linkTo).to.exist;
  });

  it("should return a valid link for Bloc", function() {
    expect(challenge.linkTo("Bloc", "http://www.bloc.io")).to.eql("<a       href='http://www.bloc.io'>Bloc</a>");
  });
});

在终端

中返回的错误消息
linkTo
✓ should be defined 
1) should return a valid link for Bloc


  1 passing (11ms)
  1 failing

  1) linkTo should return a valid link for Bloc:

  AssertionError: expected '<a href=\'Bloc\'>http://www.bloc.io</a>' to deeply equal '<a href=\'http://www.bloc.io\'>Bloc</a>'
  + expected - actual

  +<a href='http://www.bloc.io'>Bloc</a>
  -<a href='Bloc'>http://www.bloc.io</a>

  at Context.<anonymous> (/home/vagrant/frontend-javascript-exercises/02-reading-mocha-tests/00-a-tested-function/spec.js:10:63)
  at callFn (/usr/local/lib/node_modules/mocha/lib/runnable.js:266:21)
  at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:259:7)
  at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:387:10)
  at /usr/local/lib/node_modules/mocha/lib/runner.js:470:12
  at next (/usr/local/lib/node_modules/mocha/lib/runner.js:312:14)
  at /usr/local/lib/node_modules/mocha/lib/runner.js:322:7
  at next (/usr/local/lib/node_modules/mocha/lib/runner.js:257:23)
  at Object._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:289:5)
  at processImmediate [as _immediateCallback] (timers.js:336:15)

任何人都可以指出 challenge.js 中所需的更正,以便在我在终端中运行$ mocha spec.js时测试通过

提前致谢 莱克斯

1 个答案:

答案 0 :(得分:0)

用函数声明中的地址替换带有文本和文本的地址。

module.exports.linkTo = function(text, address) {   
  return "<a href='" + address + "'>" + text + "</a>"
};

测试输出清晰说明了预期的内容

+<a href='http://www.bloc.io'>Bloc</a>
-<a href='Bloc'>http://www.bloc.io</a>