Geo*_*ker -4 javascript coffeescript jasmine
我一直在学习 CoffeeScript,作为学习它的练习,我决定学习 TDD Conway 的 Game of Life。我选择的第一个测试是创建一个 Cell 并查看它是死还是活。为此,我创建了以下咖啡脚本:
class Cell
@isAlive = false
constructor: (isAlive) ->
@isAlive = isAlive
die: ->
@isAlive = false
Run Code Online (Sandbox Code Playgroud)
然后,我使用以下代码创建一个 Jasmine 测试文件(这是故意失败的测试):
Cell = require '../conway'
describe 'conway', ->
alive = Cell.isAlive
cell = null
beforeEach ->
cell = new Cell()
describe '#die', ->
it 'kills cell', ->
expect(cell.isAlive).toBeTruthy()
Run Code Online (Sandbox Code Playgroud)
但是,当我在 Jasmine 中运行测试时,出现以下错误:
cell is not defined
Run Code Online (Sandbox Code Playgroud)
和堆栈跟踪:
1) kills cell
Message:
ReferenceError: cell is not defined
Stacktrace:
ReferenceError: cell is not defined
at null.<anonymous> (/Users/gjstocker/cscript/spec/Conway.spec.coffee:17:21)
at jasmine.Block.execute (/usr/local/lib/node_modules/jasmine-node/lib/jasmine-node/jasmine-2.0.0.rc1.js:1001:15)
Run Code Online (Sandbox Code Playgroud)
当我执行coffee -c ./spec/Conway.spec.coffee
并查看生成的 JavaScript 文件时,我看到以下内容(第 17 行,第 21 列是错误):
// Generated by CoffeeScript 1.3.3
(function() {
var Cell;
Cell = require('../conway');
describe('conway', function() {
var alive, cell;
alive = Cell.isAlive;
cell = null;
return beforeEach(function() {
return cell = new Cell();
});
});
describe('#die', function() {
return it('kills cell', function() {
return expect(cell.isAlive).toBeTruthy(); //Error
});
});
}).call(this);
Run Code Online (Sandbox Code Playgroud)
我的问题是,据我所知,cell
已经定义了。我知道我错了(从那时起SELECT is not broken
),但我试图找出我哪里搞砸了。如何使用 coffescript 诊断此错误并找出哪里出错了?
我研究了许多 CoffeeScript 应用程序中包含的源代码,包括这个,但源代码的格式完全相同,声明也相同。
这是一个缩进问题,这是您的修复方法:
Cell = require '../conway'
describe 'conway', ->
alive = Cell.isAlive
cell = null
beforeEach ->
cell = new Cell()
describe '#die', ->
it 'kills cell', ->
expect(cell.isAlive).toBeTruthy()
Run Code Online (Sandbox Code Playgroud)
如果你查看编译后的 JavaScript,你会看到一个块,并且里面describe
有一个块。beforeEach
但是你的下一个describe
块(你想位于第一个块内)实际上并不在它的内部 - 它在外面。
这是因为第二个的缩进describe
只有一个空格,而不是两个。
归档时间: |
|
查看次数: |
2317 次 |
最近记录: |