2014-12-02 257 views
1

我在我的应用程序上运行'ember test --server',出现两次失败,我不知道它们为什么失败。Ember测试失败

从CLI:

> ToUrlHelper: it works 
>  ✘ Died on test #1  at eval (new-cms/tests/unit/helpers/to-url-test.js:10:5) 
>   at requireModule (http://localhost:7357/assets/vendor.js:70:29) 
>   at http://localhost:7357/assets/test-loader.js:14:29: undefined is not a function 

从Web浏览器:

> Died on test #1  at eval 
> (new-cms/tests/unit/helpers/to-url-test.js:10:5) 
>  at requireModule (http://localhost:7357/assets/vendor.js:70:29) 
>  at http://localhost:7357/assets/test-loader.js:14:29: undefined is not a function Source:  TypeError: undefined is not a function 
>  at Object.eval (new-cms/tests/unit/helpers/to-url-test.js:11:20) 
>  at Object.Test.run (http://localhost:7357/assets/test-support.js:1078:18) 
>  at http://localhost:7357/assets/test-support.js:1165:10 
>  at process (http://localhost:7357/assets/test-support.js:881:24) 
>  at http://localhost:7357/assets/test-support.js:470:5 

这是在对URL的test.js:

import { 
    toUrl 
} from 'new-cms/helpers/to-url'; 

module('ToUrlHelper'); 

// Replace this with your real tests. 
test('it works', function() { 
    var result = toUrl(42); 
    ok(result); 
}); 

从代码实际帮手:

import Ember from 'ember'; 

export default Ember.Handlebars.makeBoundHelper(function(value) { 
    if(typeof(value) !== 'undefined') { 
     return value.replace(/\s+/g, '-').toLowerCase(); 
    } 
    return ''; 
}); 

回答

1

也许这不过是你将42传递给你的函数,然后试图对其应用.replace函数。但是这个函数只能在字符串上定义...

首先传入一个字符串,例如“hello me”而不是42,然后在另一个测试中传递42,查看您的测试中断,因为您的实现没有考虑到这一点并修复您的实施。 (这是有点测试驱动开发)

+0

我使用的是ember-cli,它产生了测试,所以我不知道为什么数字'42'被传递。我应该删除测试并使用cli重新创建它吗? – Mithrilhall 2014-12-03 21:13:13

+0

嗯,我猜Ember cli只是为你生成一个基本的测试,而不知道你的函数的实现。你的测试应该是“验证”预期的行为。在你的toUrl函数中,你不希望收到数字,而是字符串。然后,你的测试必须验证意外,但“可能发生”行为,因此你应该测试传递null,未定义,数字等......到你的函数中,并修改它的实现以避免它的破坏。 (这实际上就是发生在这里的事情,烬哲提供了一个边缘案例并破坏了你的代码)。 – Sephy 2014-12-07 11:04:32

+0

如果这回答你的问题,请检查它作为正确的答案。这将验证它。 – Sephy 2014-12-07 18:09:32