Skip to content
Steve Gravrock edited this page Mar 21, 2021 · 2 revisions

The Wiki pages are deprecated! Please check jasmine.github.io for documentation.

This doesn't actually work, just some ideas:

describe("Person", function() {
  var person;

  before(function() {
    person = new Person();
  });

  when(/gender is male/, function() { person.gender = 'm'; });
  when(/gender is female/, function() { person.gender = 'f'; });

  when(/age is adult/, function() { person.isAdult = true; });
  when(/age is child/, function() { person.isAdult = false; });

  it("has an honorific appropriate to its age and gender", function() {
    when("gender is male and age is adult", function() { expect(person.honorific).toEqual("Mr."); });
    when("gender is female and age is adult", function() { expect(person.honorific).toEqual("Ms."); });
    when("gender is male and age is child", function() { expect(person.honorific).toEqual("Master"); });
    when("gender is female and age is child", function() { expect(person.honorific).toEqual("Miss"); });
  });
});

Stubbing

An idea for stubbing.. In my case, there are some tests that are browser specific, so it's nice to see what might not be tested in a given browser. This allows you to use 'stub' instead of 'it', but it also indicates that no assertions were made in a given test (so using stub is completely optional).

I just put these in my spec_helper.js file, and most of it is reliant on the TrivialReporter bit, so it's a little brittle.. it would be super nice if this was added fully as a base level feature. In ci (ruby_jasmine) it simply outputs "- stubbed", and doesn't indicate anything further, but only when 'stub' is used in place of 'it'.

jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) {
  var results = spec.results();
  var status = results.passed() ? 'passed' : 'failed';
  var style = '';
  if (results.skipped) {
    status = 'skipped';
  }
  if (results.failedCount == 0 && results.passedCount == 0 && !results.skipped) {
    status = 'stubbed';
    style = "background-color: #FFA200; border: 1px solid #000000";
  }
  var specDiv = this.createDom('div', { className: 'spec '  + status, style: style },
      this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(spec.getFullName()) }, "run"),
      this.createDom('a', {
        className: 'description',
        href: '?spec=' + encodeURIComponent(spec.getFullName()),
        title: spec.getFullName()
      }, spec.description));

  var resultItems = results.getItems();
  var messagesDiv = this.createDom('div', { className: 'messages' });
  for (var i = 0; i < resultItems.length; i++) {
    var result = resultItems[i];
    if (result.passed && !result.passed()) {
      messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage fail'}, result.message));

      if (result.trace.stack) {
        messagesDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack));
      }
    }
  }

  if (messagesDiv.childNodes.length > 0) {
    specDiv.appendChild(messagesDiv);
  }

  this.suiteDivs[spec.suite.getFullName()].appendChild(specDiv);
};

var stub = function(desc, func) {
  return jasmine.getEnv().stub(desc, func);
};

jasmine.Env.prototype.stub = function(description, func) {
  var spec = new jasmine.Spec(this, this.currentSuite, description);
  this.currentSuite.add(spec);
  this.currentSpec = spec;

  if (func) {
    spec.stub(func);
  }

  return spec;
};

jasmine.Spec.prototype.stub = function (e) {
  var expectationResult = new jasmine.MessageResult('- stubbed');
  this.results_.addResult(expectationResult);
};