Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A port of RSpec to javascript, using the Prototype javascript framework
JavaScript Ruby
branch: master

README

describe("js-spec", function() { with(this) {
  
  it("should pass when doing simple comparisons", function() {
    (1).should( equal(1) );
    (1).should( be_less_than(2) );
    (1).should( be_greater_than(0) );
    (1).should_not( equal(2) );
    (1).should_not( be(1) ); // 'be' checks if it's the same object
  });
  
  it("should pass when checking if a value is within a delta of another", function() {
    // first argument is the target, second one the maximum allowed difference between both numbers
    (1.0).should( be_close(1.05, 0.1) );
    (1.0).should_not( be_close(1.5, 0.1) );
  });
  
  it("should pass when checking if a method changes an object", function() {
    [1, 2, 3, 4].should_not( change("length").in_response_to("size") );
    // the next example would also pass if you changed 'to(3)' to 'from(4)' or 'by(-1)'
    [1, 2, 3, 4].should( change("length").to(3).in_response_to("pop") );
    // it also works for functions, and you can even pass parameters to them:
    [1, 2, 3, 4].should( change("first").to(0).in_response_to("unshift", 0) );
  });

  it("should pass when checking how many elements has an enumerable", function() {
    $("some_container").should( have(1, "childElements") );
    [1, 2, 3, 4].should( have(2, "slice", 0, 2) ); // equivalent to [1, 2, 3, 4].slice(0, 2).length.should( equal(2) )
    $("some_container").down("span").should_not( have_at_least(1, "childElements") );
    
  });
  
  it("should pass when asking if an enumerable includes some elements", function() {
    [1, 2, 3, 4].should( include(1, 2) );
    [1, 2, 3, 4].should_not( include(0) );
  })
  
  it("should match strings to regexps", function() {
    "to be or not to be".should_not( match(/^2b|[^b]{2}$/) );
    "Something".should( match(/^[Ss]/) );
  });

  it("should match DOM nodes to selectors correctly", function() {
    $("some_container").should( match("#some_container") );
    $("some_container").down(".something").should( match("#some_container .something") );
  });
  
  it("should pass when asking if an object responds to a method", function() {
    [1, 2, 3].should( respond_to("size") );
    "string".should_not( respond_to("asdfg") );
  });
  
  it("should pass when asking if an object satisfies a block", function() {
    [1, 2, 3].should( satisfy(function(list) {
      return list.length > 2;
    }));
    
    "string".should_not( satisfy(function(word) {
      return word.startsWith("cuack");
    }));
  });
  
}});
Something went wrong with that request. Please try again.