README.rdoc in visionmedia-jspec-2.7.2 vs README.rdoc in visionmedia-jspec-2.8.1

- old
+ new

@@ -9,10 +9,11 @@ == Features * Highly readable * Framework / DOM independent * Modular via JSpec Module's and hooks +* Mock Ajax Requests * Rhino support * Node.js support * Async support * Fixture support * Ruby JavaScript testing server @@ -240,10 +241,12 @@ == Helpers * Core - an_instance_of used in conjunction with the 'receive' matcher + - mockRequest mock a request (requires jspec.xhr.js) + - unmockRequest unmock requests (requests jspec.xhr.js) * jQuery - sandbox used to generate new DOM sandbox, using jQuery object - element same as invoking jQuery, just reads better and no need to worry about $ collisions @@ -279,10 +282,33 @@ end end NOTE: both User and Administrator's before hooks implement the 'user' variable +== Mock Ajax Requests + +JSpec supports generic Ajax mocking which is usable with any JavaScript framework via 'jspec.xhr.js'. The +API is comprised of two functions, mockRequest() and unmockRequest(). unmockRequest() is +automatically called after each specification to restore the default functionality of XMLHttpRequest, +so it is uncommon to call unmockRequest() directly. Below is a jQuery example: + + it 'should mock requests' + mockRequest().and_return('{ foo : "bar" }', 'application/json') + $.getJSON('foo', function(response, statusText){ + response.foo.should.eql 'bar' + }) + end + +The mockRequest().and_return signature is as follows: + + mockRequest().and_return(<data>, [content-type], [response-status-code], [headers-hash]) + +At the moment mockRequest() itself does not accept any arguments, however in the future +this will be used to target specific uris for mocking. + +NOTE: works with Rhino as well + == Hooks Currently the following hooks are supported, and may be utilized any number of times as they are simply pushed to a stack. So for instance you may have two before_each blocks within the same scope, they will both run, but this can help keep your specs readable. @@ -395,10 +421,43 @@ where as 'spec/fixtures/xml/data.xml' must be specified with fixture('xml/data.xml'). If you prefer not to store fixtures in the 'fixtures' directory you must be more specific with the path supplied. +== Testing DOM Elements + +When using jQuery testing DOM elements is very easy. Many may think they require specific +sandbox divs in their html, however you do not. Using the fixture support mentioned above +you may simply load some HTML, and use the 'elements()' utility which is an alias of jQuery: + + describe 'JSpec DOM testing' + describe 'is so easy' + before_each + list = elements(fixture('users-list')) + // or list = jQuery(fixture('users-list')) + // or list = $(fixture('users-list')) + end + + it 'should have users' + list.should.have_tag 'ul' + end + end + end + +You may also use simple strings, since jQuery's constructor will convert them to DOM elements: + + describe 'Something' + before_each + html = elements('<p>Foo</p>') + // or html = $('<p>Foo</p>') ... + end + + it 'should do something' + html.should.have_text 'Foo' + end + end + == Custom Matchers First lets create a simple equality matcher. In the case below JSpec is smart enough to realize this is simply a binary operator, and simply transforms this into 'actual === expected' @@ -630,9 +689,10 @@ Many ideas and bug reports were contributed by the following developers, thankyou for making JSpec more enjoyable, and bug free ;) +* Lawrence Pit * mpd@jesters-court.ne * kevin.gisi@gmail.com * enno84@gmx.net == License