【问题标题】:Why shouldjs says the two objects are not the same?为什么 shouldjs 说这两个对象不一样?
【发布时间】:2015-10-20 17:31:45
【问题描述】:

我是coffeescript 的新手,我尝试为coffeescript 和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,则返回true
  • fn 上申请id。我希望结果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 做了一些意想不到的事情,还是咖啡脚本没有被翻译成我希望它翻译成的代码?

【问题讨论】:

  • 您确定问题出在f 上吗? should.js documentation 注意到 be.false 在版本 7 中更改为 be.false()。也许您还在使用旧版本?
  • @andersschuller 在那之前就失败了,所以不,这不是问题(但可能是问题,谁知道)。

标签: coffeescript should.js


【解决方案1】:

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

【讨论】:

  • 这很有趣。我有一个should = require 'should',它是文件中其他地方的代码的咖啡脚本等价物,我还有其他测试通过并检查与 should.js 的简单比较。我会看看我是否会以某种方式掩盖它。
【解决方案2】:

我看了一下,无法重现问题。

问题:

  • 第 31 行是什么语句? (行号和 console.log 可以说明这里发生了什么)。
  • 为什么会出现 TypeError(它试图将对象作为函数调用)
  • 文件编译成什么(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.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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多