0

I am a beginner of Haxe. I tried to do FizzBuzz TDD. I used Mocha and expect.js. I exposed the Haxe class by @:expose("SomeName") to be able to be seen from test.

However, the test cannot find FizzBuzz class.

FizzBuzz.hx

package ;

@:expose("SomeName")
class FizzBuzz{

    public function new() {

    }

    public function put(n : Int) : String {
        if (n == 3) {
            return "Fizz";
        }
        else {
            return Std.string(n);
        }
    }
}

Main.hx

package ;

import js.Lib;

class Main {

    static function main() {
        var f = new FizzBuzz();
        for (i in 1...100) {
            f.put(i);
        }

    }

}

fizzbuzz.js

(function ($hx_exports) { "use strict";
var FizzBuzz = $hx_exports.SomeName = function() {
};
FizzBuzz.prototype = {
    put: function(n) {
        if(n == 3) return "Fizz"; else if(n == null) return "null"; else return "" + n;
    }
};
var Main = function() { };
Main.main = function() {
    var f = new FizzBuzz();
    var _g = 1;
    while(_g < 100) {
        var i = _g++;
        f.put(i);
    }
};
Main.main();
})(typeof window != "undefined" ? window : exports);

fizzbuzztest.coffee

expect = require 'expect.js'
fizzbuzz   = require '../bin/fizzbuzz.js'

describe 'fizzbuzz', ->
    it 'put Fizz when 3 is given', ->
        f = new SomeName()
        result = f.put(3)
        expect(result).to.be(3)

Error Message

fizzbuzz put Fizz when 3 is given
    ✘ failed
        ReferenceError: SomeName is not defined

How can I find the FizzBuzz class from test code? Thank you for your help.

Feel Physics
  • 2,783
  • 4
  • 25
  • 38
  • This doesn't answer your question, but a more straight forward approach might be to use a native Haxe BDD library. There's a new one called "Buddy" that I quite like so far: https://github.com/ciscoheat/buddy – Jason O'Neil Jul 24 '14 at 08:30

1 Answers1

1

fizzbuzz = require '../bin/fizzbuzz.js'

SomeName was exported to your exports, so it is currenly in fizzbuzz.SomeName and of course there is no variable named SomeName(and so it says to you).

Actually, your problem have nothing to do with haxe, it's you just incorrect coffeescript code you written, it wouldn't work with any native js module too.

stroncium
  • 1,430
  • 9
  • 8