Javascript Unit Testing

  • 1,108 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,108
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
18
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Javascript Unit Testing Jakub Suder <jakub.suder@gmail.com> http://psionides.jogger.pl
  • 2. Różne biblioteki
      • jsUnitTest
    new Test.Unit.Runner({ testCalculatorAdd: function() { with (this) { var calc = new Calculator(); assertEqual(4, calc.add(2, 2)); } } });
  • 3. 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)); }); } });
  • 4. Różne biblioteki
      • JSpec
    describe 'Calculator' it 'should add numbers' var calc = new Calculator calc.add(2, 2).should.equal 4 end end
  • 5. Różne biblioteki
      • jqUnit
      • unittest
      • YUItest
      • jsUnit
      • FireUnit
      • QUnit
      • RhinoUnit
      • ...
  • 6. Blue Ridge
      • plugin Rails
      • Screw.Unit
      • Smoke
      • Rhino
      • Env.js
  • 7. 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
  • 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
      • wyniki testu - przeglądarka:
  • 14. 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
  • 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 :-)