Javascript Unit Testing Jakub Suder <jakub.suder@gmail.com> http://psionides.jogger.pl
Różne biblioteki jsUnitTest new Test.Unit.Runner({ testCalculatorAdd: function() { with (this) { var calc = new Calculator(); assertEqual(4, calc.add(2, 2)); } } });
Różne biblioteki JSSpec describe(&quot;Calculator&quot;, function() { with(this) { it(&quot;should add numbers&quot;, function() { var calc = new Calculator(); calc.add(2, 2).should(equal(4)); }); } });
Różne biblioteki JSpec describe 'Calculator' it 'should add numbers' var calc = new Calculator calc.add(2, 2).should.equal 4 end end
Różne biblioteki jqUnit unittest YUItest jsUnit FireUnit QUnit RhinoUnit ...
Blue Ridge plugin Rails Screw.Unit Smoke Rhino Env.js
Blue Ridge ./script/plugin install git://github.com/relevance/blue-ridge.git ./script/generate blue_ridge ./script/generate javascript_spec event rake spec:javascripts [TEST=event] rake js:shell
Blue Ridge / |- spec | |- javascripts | |- event_spec.js |- utils_spec.js |- spec_helper.js |- ... |- fixtures |- event.html |- utils.html |- ...
Blue Ridge (Screw.Unit) // utils_spec.js require(&quot;spec_helper.js&quot;); require(&quot;../../public/javascripts/utils.js&quot;);  Screw.Unit(function() { describe(&quot;Utils&quot;, function() { it(&quot;does something&quot;, function() { expect(&quot;hello&quot;).to(equal, &quot;hello&quot;); }); }); });
Blue Ridge (Screw.Unit) expect(something).to(equal, expected) expect(something).to_not(equal, expected) ... be_true / be_false be_null / be_undefined have_length, n be_empty match, 'str' match, /regexp/ be_gt / be_lt / be_gte / be_lte, val Screw.Matchers[&quot;be_visible&quot;] = { ... }
Blue Ridge (Smoke) mock(something).should_receive('method'). with_arguments(&quot;x&quot;, 1).exactly('once'). and_return(0) stub(something, 'method').and_return(2) stub(something, 'field').and_set_to(3)
Blue Ridge // fixtures/utils.html (w skrócie) <html> <head> <script type=&quot;text/javascript&quot; src=&quot;../../../vendor/plugins/blue_ridge /lib/blue_ridge.js&quot;></script> </head> <body> <!-- Put any HTML fixture elements here. --> </body> </html>
Blue Ridge wyniki testu - przeglądarka:
Blue Ridge wyniki testu - konsola: $ rake spec:javascripts TEST=event Running event_spec.js with fixture 'fixtures/event.html'... ........................... 27 test(s), 0 failure(s) 0.453 seconds elapsed
Blue Ridge $ rake js:shell ================================================= Rhino JavaScript Shell To exit type 'exit', 'quit', or 'quit()'. ================================================= - loaded env.js - sample DOM loaded - jQuery-1.2.6 loaded ================================================= Rhino 1.7 release 2 PRERELEASE 2008 07 28 js> $('body').append(&quot;<p>asd</p>&quot;) [object Object] js> $('p').length 1 js>
Blue Ridge Uwagi: Rhino + env.js ≠ przeglądarka bugi w env.js Smoke.reset() nie resetuje :-)

Javascript Unit Testing

  • 1.
    Javascript Unit TestingJakub Suder <jakub.suder@gmail.com> http://psionides.jogger.pl
  • 2.
    Różne biblioteki jsUnitTestnew Test.Unit.Runner({ testCalculatorAdd: function() { with (this) { var calc = new Calculator(); assertEqual(4, calc.add(2, 2)); } } });
  • 3.
    Różne biblioteki JSSpecdescribe(&quot;Calculator&quot;, function() { with(this) { it(&quot;should add numbers&quot;, function() { var calc = new Calculator(); calc.add(2, 2).should(equal(4)); }); } });
  • 4.
    Różne biblioteki JSpecdescribe 'Calculator' it 'should add numbers' var calc = new Calculator calc.add(2, 2).should.equal 4 end end
  • 5.
    Różne biblioteki jqUnitunittest YUItest jsUnit FireUnit QUnit RhinoUnit ...
  • 6.
    Blue Ridge pluginRails Screw.Unit Smoke Rhino Env.js
  • 7.
    Blue Ridge ./script/plugininstall git://github.com/relevance/blue-ridge.git ./script/generate blue_ridge ./script/generate javascript_spec event rake spec:javascripts [TEST=event] rake js:shell
  • 8.
    Blue Ridge /|- spec | |- javascripts | |- event_spec.js |- utils_spec.js |- spec_helper.js |- ... |- fixtures |- event.html |- utils.html |- ...
  • 9.
    Blue Ridge (Screw.Unit)// utils_spec.js require(&quot;spec_helper.js&quot;); require(&quot;../../public/javascripts/utils.js&quot;); Screw.Unit(function() { describe(&quot;Utils&quot;, function() { it(&quot;does something&quot;, function() { expect(&quot;hello&quot;).to(equal, &quot;hello&quot;); }); }); });
  • 10.
    Blue Ridge (Screw.Unit)expect(something).to(equal, expected) expect(something).to_not(equal, expected) ... be_true / be_false be_null / be_undefined have_length, n be_empty match, 'str' match, /regexp/ be_gt / be_lt / be_gte / be_lte, val Screw.Matchers[&quot;be_visible&quot;] = { ... }
  • 11.
    Blue Ridge (Smoke)mock(something).should_receive('method'). with_arguments(&quot;x&quot;, 1).exactly('once'). and_return(0) stub(something, 'method').and_return(2) stub(something, 'field').and_set_to(3)
  • 12.
    Blue Ridge //fixtures/utils.html (w skrócie) <html> <head> <script type=&quot;text/javascript&quot; src=&quot;../../../vendor/plugins/blue_ridge /lib/blue_ridge.js&quot;></script> </head> <body> <!-- Put any HTML fixture elements here. --> </body> </html>
  • 13.
    Blue Ridge wynikitestu - przeglądarka:
  • 14.
    Blue Ridge wynikitestu - konsola: $ rake spec:javascripts TEST=event Running event_spec.js with fixture 'fixtures/event.html'... ........................... 27 test(s), 0 failure(s) 0.453 seconds elapsed
  • 15.
    Blue Ridge $rake js:shell ================================================= Rhino JavaScript Shell To exit type 'exit', 'quit', or 'quit()'. ================================================= - loaded env.js - sample DOM loaded - jQuery-1.2.6 loaded ================================================= Rhino 1.7 release 2 PRERELEASE 2008 07 28 js> $('body').append(&quot;<p>asd</p>&quot;) [object Object] js> $('p').length 1 js>
  • 16.
    Blue Ridge Uwagi:Rhino + env.js ≠ przeglądarka bugi w env.js Smoke.reset() nie resetuje :-)