我尝试将 mocha 与 coffeescript 和 chai 结合使用。在每个包含测试的文件之前,我想包含以下文件:
测试/_helper.coffee
path = require 'path'
AppDir = path.resolve("#{__dirname}/../src/app")
chai = require('chai')
should = chai.should()
factories = require('chai-factories')
chai.use(factories)
这样我就可以访问 AppDir 变量。这样当我需要一个文件时,我就不必指定应用程序目录的完整路径。
测试/应用程序/setup-test.coffee
describe 'Setup instance', ->
it 'should be a object', ->
setup = require "#{AppDir}/setup"
setup.should.be.a('object')
我尝试了以下设置:
将 _hellper.coffee 添加到 mocha 命令行选项,如下所示:
./node_modules/.bin/mocha --require coffee-script --require test/_helper.coffee --compilers coffee:coffee-script --recursive --reporter spec test
所以:
./node_modules/.bin/mocha --require coffee-script --compilers coffee:coffee-script --recursive --reporter spec test/_helper.coffee test
我在 setup-test.coffee 中尝试了一个正常的要求:
require '../_helper.coffee'
无论我使用什么方法,如果我运行测试,我都会收到以下错误:
Setup instance
1) should be a object
✖ 1 of 1 test failed:
1) Setup instance should be a object:
ReferenceError: AppDir is not defined
at Context.<anonymous> (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/test/app/setup-test.coffee:8:28)
at Test.Runnable.run (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runnable.js:184:32)
at Runner.runTest (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runner.js:300:10)
at Runner.runTests.next (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runner.js:346:12)
at next (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runner.js:228:14)
at Runner.hooks (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runner.js:237:7)
at next (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runner.js:185:23)
at Runner.hook (/Users/ivotrompert/Dropbox/projects/nodejs/getWallpapers/node_modules/mocha/lib/runner.js:205:5)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
如果有人想知道我如何运行测试,这是我的 MAKE 文件:
生成文件
REPORTER = spec
DEFAULT_COMMAND = ./node_modules/.bin/mocha --require coffee-script --ui bdd --compilers coffee:coffee-script --recursive --growl --reporter
check: test
test:
@NODE_ENV=test $(DEFAULT_COMMAND) $(REPORTER)
test-watch:
@clear
@NODE_ENV=test $(DEFAULT_COMMAND) $(REPORTER) --watch
coverage:
@jscoverage --no-highlight src src-cov
@SRC_COV=1 $(DEFAULT_COMMAND) html-cov > coverage.html
@rm -rf src-cov
ifeq ($(shell uname), Darwin)
@open coverage.html
else
@xdg-open coverage.html &> /dev/null &
endif
.PHONY: test
有人可以帮助我吗,谢谢。