Skip to content
jejacks0n edited this page Jan 27, 2013 · 19 revisions

Teabag makes it pretty easy to use Mocha for your tests, and includes some nice support libraries that you might find handy.

To get setup for Mocha just configure your suites to use Mocha instead of Jasmine. Here's an example of changing the default suite to use Mocha.

config/initializers/teabag.rb

Teabag.setup do |config|

  config.suite do |suite|
    suite.javascripts = ["teabag/mocha", "support/expect", "support/sinon"]
  end

end

You'll notice that we also included expect.js and sinon.js in our base -- these are useful libraries that we like, but you're free to use others like should.js. You should technically require support libraries from within your spec helper -- which is advised, but to simplify this article we did it in one step.

If you're into Chai, we include it as a support library as well. And you should check out the konacha-chai-matchers project -- there's some good stuff in there and you can easily utilize the features there within Teabag.

You'll have to do this for all the suites you create where you want to use Mocha.

Example Spec

//= require jquery
describe("My great feature", function() {

  it("will change the world", function() {
    expect(true).to.be(true);
    expect(jQuery).to.not.be(undefined);
  });

});

Example Fixture Usage

fixture.preload("fixture.html", "fixture.json");

describe("Using fixtures", function() {
  fixture.set("<h2>Another Title</h2>");

  beforeEach(function() {
    this.fixtures = fixture.load("fixture.html", "fixture.json", true);
  });

  it("loads fixtures", function() {
    expect(document.getElementById("fixture_view").tagName).to.be("DIV");
    expect(this.fixtures[0]).to.be(fixture.el);
    expect(this.fixtures[1]).to.eql(fixture.json[0]);
  });
});