Skip to content
jejacks0n edited this page Dec 30, 2012 · 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.

Check out the konacha-chai-matchers project -- there's some good stuff in there.

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") # make the actual requests for the files
describe "Using fixtures", ->

  fixture.set("<h2>Another Title</h2>") # create some markup manually (will be in a beforeEach)

  beforeEach ->
    @fixtures = fixture.load("fixture.html", "fixture.json", true) # append these fixtures

  it "loads fixtures", ->
    expect(document.getElementById("fixture_view").tagName).to.be("DIV")
    expect(@fixtures[0]).to.be(fixture.el) # the element is available as a return value and through fixture.el
    expect(@fixtures[1]).to.eql(fixture.json[0]) # the json for json fixtures is returned, and available in fixture.json