Javascript Unit Testing Jakub Suder <jakub.suder@gmail.com> http://psionides.jogger.pl
Różne biblioteki <ul><ul><li>jsUnitTest </li></ul></ul>new Test.Unit.Runner({ testCalculatorAdd: function() { with (this) ...
Różne biblioteki <ul><ul><li>JSSpec </li></ul></ul>describe(&quot;Calculator&quot;, function() { with(this) { it(&quot;sho...
Różne biblioteki <ul><ul><li>JSpec </li></ul></ul>describe 'Calculator' it 'should add numbers' var calc = new Calculator ...
Różne biblioteki <ul><ul><li>jqUnit </li></ul></ul><ul><ul><li>unittest </li></ul></ul><ul><ul><li>YUItest </li></ul></ul>...
Blue Ridge <ul><ul><li>plugin Rails </li></ul></ul><ul><ul><li>Screw.Unit </li></ul></ul><ul><ul><li>Smoke </li></ul></ul>...
Blue Ridge <ul><li>./script/plugin install </li></ul><ul><li>git://github.com/relevance/blue-ridge.git </li></ul><ul><li>....
Blue Ridge <ul><li>/ </li></ul><ul><li>|- spec </li></ul><ul><li>| </li></ul><ul><li>|- javascripts </li></ul><ul><li>| </...
Blue Ridge (Screw.Unit) <ul><li>// utils_spec.js </li></ul><ul><li>require(&quot;spec_helper.js&quot;); </li></ul><ul><li>...
Blue Ridge (Screw.Unit) <ul><li>expect(something).to(equal, expected) </li></ul><ul><li>expect(something).to_not(equal, ex...
Blue Ridge (Smoke) <ul><li>mock(something).should_receive('method'). </li></ul><ul><li>with_arguments(&quot;x&quot;, 1).ex...
Blue Ridge <ul><li>// fixtures/utils.html (w skrócie) </li></ul><ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li>...
Blue Ridge <ul><ul><li>wyniki testu - przeglądarka: </li></ul></ul>
Blue Ridge <ul><ul><li>wyniki testu - konsola: </li></ul></ul>$ rake spec:javascripts TEST=event Running event_spec.js wit...
Blue Ridge $ rake js:shell ================================================= Rhino JavaScript Shell To exit type 'exit', '...
Blue Ridge <ul><li>Uwagi: </li></ul><ul><ul><li>Rhino + env.js ≠ przeglądarka </li></ul></ul><ul><ul><li>bugi w env.js </l...
Upcoming SlideShare
Loading in …5
×

Javascript Unit Testing

1,302 views
1,258 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,302
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
19
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Javascript Unit Testing

  1. 1. Javascript Unit Testing Jakub Suder <jakub.suder@gmail.com> http://psionides.jogger.pl
  2. 2. Różne biblioteki <ul><ul><li>jsUnitTest </li></ul></ul>new Test.Unit.Runner({ testCalculatorAdd: function() { with (this) { var calc = new Calculator(); assertEqual(4, calc.add(2, 2)); } } });
  3. 3. Różne biblioteki <ul><ul><li>JSSpec </li></ul></ul>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)); }); } });
  4. 4. Różne biblioteki <ul><ul><li>JSpec </li></ul></ul>describe 'Calculator' it 'should add numbers' var calc = new Calculator calc.add(2, 2).should.equal 4 end end
  5. 5. Różne biblioteki <ul><ul><li>jqUnit </li></ul></ul><ul><ul><li>unittest </li></ul></ul><ul><ul><li>YUItest </li></ul></ul><ul><ul><li>jsUnit </li></ul></ul><ul><ul><li>FireUnit </li></ul></ul><ul><ul><li>QUnit </li></ul></ul><ul><ul><li>RhinoUnit </li></ul></ul><ul><ul><li>... </li></ul></ul>
  6. 6. Blue Ridge <ul><ul><li>plugin Rails </li></ul></ul><ul><ul><li>Screw.Unit </li></ul></ul><ul><ul><li>Smoke </li></ul></ul><ul><ul><li>Rhino </li></ul></ul><ul><ul><li>Env.js </li></ul></ul>
  7. 7. Blue Ridge <ul><li>./script/plugin install </li></ul><ul><li>git://github.com/relevance/blue-ridge.git </li></ul><ul><li>./script/generate blue_ridge </li></ul><ul><li>./script/generate javascript_spec event </li></ul><ul><li>rake spec:javascripts [TEST=event] </li></ul><ul><li>rake js:shell </li></ul>
  8. 8. Blue Ridge <ul><li>/ </li></ul><ul><li>|- spec </li></ul><ul><li>| </li></ul><ul><li>|- javascripts </li></ul><ul><li>| </li></ul><ul><li>|- event_spec.js </li></ul><ul><li>|- utils_spec.js </li></ul><ul><li>|- spec_helper.js </li></ul><ul><li>|- ... </li></ul><ul><li>|- fixtures </li></ul><ul><li>|- event.html </li></ul><ul><li>|- utils.html </li></ul><ul><li>|- ... </li></ul>
  9. 9. Blue Ridge (Screw.Unit) <ul><li>// utils_spec.js </li></ul><ul><li>require(&quot;spec_helper.js&quot;); </li></ul><ul><li>require(&quot;../../public/javascripts/utils.js&quot;); </li></ul><ul><li>Screw.Unit(function() { </li></ul><ul><li>describe(&quot;Utils&quot;, function() { </li></ul><ul><li>it(&quot;does something&quot;, function() { </li></ul><ul><li>expect(&quot;hello&quot;).to(equal, &quot;hello&quot;); </li></ul><ul><li>}); </li></ul><ul><li>}); </li></ul><ul><li>}); </li></ul>
  10. 10. Blue Ridge (Screw.Unit) <ul><li>expect(something).to(equal, expected) </li></ul><ul><li>expect(something).to_not(equal, expected) </li></ul><ul><li>... be_true / be_false </li></ul><ul><li>be_null / be_undefined </li></ul><ul><li>have_length, n </li></ul><ul><li>be_empty </li></ul><ul><li>match, 'str' </li></ul><ul><li>match, /regexp/ </li></ul><ul><li>be_gt / be_lt / be_gte / be_lte, val </li></ul><ul><li>Screw.Matchers[&quot;be_visible&quot;] = { ... } </li></ul>
  11. 11. Blue Ridge (Smoke) <ul><li>mock(something).should_receive('method'). </li></ul><ul><li>with_arguments(&quot;x&quot;, 1).exactly('once'). </li></ul><ul><li>and_return(0) </li></ul><ul><li>stub(something, 'method').and_return(2) </li></ul><ul><li>stub(something, 'field').and_set_to(3) </li></ul>
  12. 12. Blue Ridge <ul><li>// fixtures/utils.html (w skrócie) </li></ul><ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><script type=&quot;text/javascript&quot; </li></ul><ul><li>src=&quot;../../../vendor/plugins/blue_ridge </li></ul><ul><li>/lib/blue_ridge.js&quot;></script> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><!-- Put any HTML fixture elements here. --> </body> </li></ul><ul><li></html> </li></ul>
  13. 13. Blue Ridge <ul><ul><li>wyniki testu - przeglądarka: </li></ul></ul>
  14. 14. Blue Ridge <ul><ul><li>wyniki testu - konsola: </li></ul></ul>$ 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. 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. 16. Blue Ridge <ul><li>Uwagi: </li></ul><ul><ul><li>Rhino + env.js ≠ przeglądarka </li></ul></ul><ul><ul><li>bugi w env.js </li></ul></ul><ul><ul><li>Smoke.reset() nie resetuje :-) </li></ul></ul>

×