我是coffeescript的新手,我尝试创建一个库,为cofffeescript和javascript添加一些语法糖。它使用了很多装饰器,所以我很惊讶这个测试失败了:
it 'sandbox', () ->
id = (x) -> x
fn = (y) -> y == 1
f = id fn
should(f).be.equal(fn)
should(f 3).be.false()
我认为我在做什么:
id
。fn
,如果第一个参数为1
id
上申请fn
。我希望结果f
与fn
完全相同(参考明智!)。 should.js说我的结果f
甚至不是函数:
1) Function guard predicate #bakeFunctionPredicate sandbox:
TypeError: object is not a function
at Context.<anonymous> (/Users/luftzug/private/jspatterns/test/patterns.test.coffee:31:7)
at Test.Runnable.run (/Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runnable.js:221:32)
at Runner.runTest (/Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runner.js:378:10)
at /Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runner.js:456:12
at next (/Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runner.js:303:14)
at /Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runner.js:313:7
at next (/Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runner.js:251:23)
at Immediate._onImmediate (/Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runner.js:280:5)
at processImmediate [as _immediateCallback] (timers.js:367:17)
我很困惑。是shouldjs
是否有意外的事情,或者coffeescript没有被翻译成我希望它翻译成的代码?
答案 0 :(得分:0)
堆栈中的错误不是AssertionError,这意味着问题不在于should.js本身。我将代码从咖啡转换为js,看起来是正确的。我认为你的测试中没有should
函数:
var should = require('should')
答案 1 :(得分:0)
我看了一眼,无法重现这个问题。
问题:
你必须在外面展示一些东西(旧包版本?,grunt-mocha-cli配置?,???)
这是我做的验证。注意:所有npm包都是全新安装。
我从您的基本功能开始,并证明了&#39; 我认为我在做什么&#39;真。
id = (x) -> x
fn = (y) -> y == 1
console.log "typeof fn: #{typeof fn}" # function
f = id fn
console.log "typeof f: #{typeof f}" # function
console.log "fn is f: #{fn is f}" # true
console.log "fn == f: #{fn == f}" # true
# CoffeeScript thinks they are equal
然后我测试了那应该&#39;断言传递
should(f).be.equal(fn)
should(f 3).be.false()
# No assertions here
然后尝试重新创建您的测试
describe 'function comparison', ->
it 'should provide identity for functions', ->
id = (x) -> x
fn = (y) -> y == 1
f = id fn
should(f).be.equal(fn)
should(f 3).be.false()
使用此Gruntfile.coffee
module.exports = (grunt) ->
require('load-grunt-tasks')(grunt)
grunt.initConfig
pkg: grunt.file.readJSON 'package.json'
mochacli:
options:
require: ['should', 'coffee-script/register']
reporter: 'spec'
compilers: ['coffee:coffee-script']
all: ['coffeescript/functionComparison.coffee']
# run test for functionComparison.coffee
grunt.registerTask 'default', ['mochacli']
产生
Running "mochacli:all" (mochacli) task
function comparison
✓ should provide identity for functions
1 passing (3ms)
Done, without errors.