2015-10-20 24 views
1

我是coffeescript的新手,我尝试为cofffeescript和javascript添加一些语法糖库。它采用了大量的装饰,让我很惊讶,这个测试失败:为什么shouldjs说这两个对象不一样?

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
  • fn适用id返回true。我期望结果ffn完全相同(参考明智!)。

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

你确定这个问题是'F'? [should.js文档](https://github.com/shouldjs/should.js/wiki/Breaking-changes)注意到在版本7中'be.false'变成了'be.false()'。也许你仍在使用旧版本? – andersschuller

+0

@andersschuller在此之前它没有成功,所以不,这不是问题(但可能是一个问题,谁知道)。 – Luftzig

回答

0

堆栈错误不是AssertionError,这意味着问题不在should.js本身。我将代码从咖啡转换为js,看起来正确。我认为你没有should功能在你的测试中: var should = require('should')

+0

这很有趣。我有一个'should = require'should'',这是你的代码在文件其他地方的等价代码,我有其他的测试可以通过并检查与should.js的简单比较。我会看看,如果我可能会影响它如何。 – Luftzig

0

我看了看,并且无法重现问题。

问题:

  • 什么说法是在第31行? (行号和console.log可以说明这里发生了什么)。
  • 为什么类型错误(它正试图调用对象作为函数)
  • 是什么文件编译为(不JavaScript的显示问题更清晰)

一定有一些事情以外什么你已经显示(旧包版本?,grunt-mocha-cli配置?)

这是我做的验证。注意:所有npm包都是全新安装。

我从你的基本功能开始,并证明'我认为我在做什么'是真的。

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 

然后,我测试了 '应该' 主张通过

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。咖啡

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. 
相关问题