2015-10-20 23 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. 
相關問題