-4

私はコーヒースクリプトを学んでおり、それを学ぶための練習として、TDD Conway's Game of Lifeに決めました。私が選択した最初のテストは、Cell を作成し、それが死んでいるか生きているかを確認することです。そのために、次の coffeescript を作成しました。

class Cell 
  @isAlive = false


  constructor: (isAlive) ->
    @isAlive = isAlive

  die: ->
    @isAlive = false

次に、次のコードを使用して Jasmine テスト ファイルを作成しています (これは意図的に失敗したテストです)。

Cell = require '../conway'

describe 'conway', ->
  alive = Cell.isAlive
  cell = null

  beforeEach ->
    cell = new Cell()

 describe '#die', ->
   it 'kills cell', ->
     expect(cell.isAlive).toBeTruthy()

ただし、Jasmine でテストを実行すると、次のエラーが発生します。

cell is not defined

そしてスタックトレース:

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)

実行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);

私の問題は、私が知る限り、定義されているということcell です。私は自分が間違っていることを知っています (以来SELECT is not broken), しかし、私はどこを台無しにしたのかを理解しようとしています. coffescript を使用してこのエラーを診断し、どこで問題が発生したかを特定するにはどうすればよいですか?

これを含む多くの coffeescript アプリに含まれるソース コードを調べました、ソース コードの形式はまったく同じで、宣言も同じです。

4

1 に答える 1

4

これはインデントの問題であり、修正は次のとおりです。

Cell = require '../conway'

describe 'conway', ->
  alive = Cell.isAlive
  cell = null

  beforeEach ->
    cell = new Cell()

  describe '#die', ->
    it 'kills cell', ->
      expect(cell.isAlive).toBeTruthy()

コンパイルされた JavaScript を見ると、describeブロックがあり、そのbeforeEach中に があります。しかし、次のdescribeブロック (最初のブロックの中に入れたかった) は、実際にはそのブロックの内側ではなく、外側にあります

これは、2 番目のインデントが 2describeつではなく 1 つのスペースだけであるためです。

于 2012-08-20T03:01:17.287 に答える