Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Testing JavaScript in Rails Applications
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Vasili Kachalko
February 23, 2013
Programming
500
6
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Testing JavaScript in Rails Applications
Vasili Kachalko
February 23, 2013
Other Decks in Programming
See All in Programming
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
290
AIを活用したE2Eテスト実装効率化のあゆみ / ebisu-mobile-14-kotetu
kotetuco
0
130
エージェンティックRAGにAWSで入門しよう!
har1101
9
1.7k
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
390
Oxlintのカスタムルールの現況
syumai
6
1.1k
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
130
Agentic UI
manfredsteyer
PRO
0
190
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
120
1B+ /day規模のログを管理する技術
broadleaf
0
110
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.7k
RTSPクライアントを自作してみた話
simotin13
0
620
JavaDoc 再入門
nagise
1
380
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
57
14k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
440
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
150
Amusing Abliteration
ianozsvald
1
210
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
360
Marketing to machines
jonoalderson
1
5.5k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
400
Why Our Code Smells
bkeepers
PRO
340
58k
Designing for Timeless Needs
cassininazir
1
260
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
180
Transcript
TESTING JS IN RAILS APPS and H. J. Simpson Vasili
Kachalko twitter.com/dreamfa11 github.com/dreamfall Brainspec brainspec.com Sunday, February 24, 13
CLIENT SIDE APPS PROS Sunday, February 24, 13
CLIENT SIDE APPS PROS • High speed Sunday, February 24,
13
CLIENT SIDE APPS PROS • High speed • Clients love
them Sunday, February 24, 13
CLIENT SIDE APPS PROS • High speed • Clients love
them • Server load reduction Sunday, February 24, 13
CLIENT SIDE APPS CONS Sunday, February 24, 13
CLIENT SIDE APPS CONS • Testing Sunday, February 24, 13
CLIENT SIDE APPS CONS • Testing • It’s JavaScript Sunday,
February 24, 13
TEST COVERAGE 100% Sunday, February 24, 13
APPLICATION LANGUAGES 63% 7% 30% ruby javascript coffeescript Sunday, February
24, 13
SO HOW SHOULD WE TEST JS Capybara? Sunday, February 24,
13
SO HOW SHOULD WE TEST JS Capybara? NO Sunday, February
24, 13
SOLUTIONS mocha* + chai * - mocha читается как “мока”.
Точно. Я проверял. Да, другой вариант смешнее. Sunday, February 24, 13
Sunday, February 24, 13
Sunday, February 24, 13
ASSERTIONS/MATCHERS Sunday, February 24, 13
ASSERTIONS/MATCHERS • to (should) • be • been • is
• that • and • have • with • .deep • .a(type) • .include(value) • .ok • .true • .false • .null • .undefined • .exist • .empty • .equal (.eql) • .above(value) • .below(value) • .within(start, finish) • .instanceof(constructor) • .property(name, [value]) • .ownProperty(name) • .length(value) • .match(regexp) • .string(string) • .keys(key1, [key2], [...]) • .throw(constructor) • .respondTo(method) • .satisfy(method) • .closeTo(expected, delta) Sunday, February 24, 13
EXAMPLE describe 'Foo', -> it 'returns omg', -> expect(Foo.bar).to.eq('omg') Foo.bar.should.eq('omg')
assert_equal(Foo.bar, 'omg') Sunday, February 24, 13
INTEGRATING WITH RAILS Sunday, February 24, 13
Sunday, February 24, 13
INSTALLATION # Gemfile gem 'konacha' # config/initializers/konacha.rb Konacha.configure do |config|
config.spec_dir = "spec/javascripts" config.spec_matcher = /_spec\.|_test\./ config.driver = :selenium config.stylesheets = %w(application) end if defined?(Konacha) Sunday, February 24, 13
EXAMPLE <div class="choo"> <input type="text"><a href="#">Do it</a> </div> app/views/home/index.html.erb Sunday,
February 24, 13
EXAMPLE app/assets/javascripts/application.js.coffee #= require jquery jQuery -> ($ document).delegate 'a',
'click', (e) -> do e.preventDefault ($ '.choo input').val 'CHOO CHOO!11' Sunday, February 24, 13
EXAMPLE spec/javascripts/spec_helper.js.coffee #= require_tree ./templates #= require application #= require
chai-jquery Sunday, February 24, 13
EXAMPLE spec/javascripts/templates/choo.jst.skim .choo input type="text" a href="#" | Do it
Sunday, February 24, 13
EXAMPLE spec/javascripts/application_spec.js.coffee #= require spec_helper describe 'Application', -> it 'works!',
-> ($ '#konacha').html JST['templates/choo']() do ($ '.choo a').click expect($ '.choo input').to.have.value 'CHOO CHOO!11' Sunday, February 24, 13
RUNNING rake konacha:run Sunday, February 24, 13
RUNNING rake konacha:serve http://0.0.0.0:3500/ Sunday, February 24, 13
EXAMPLE spec/javascripts/templates/choo.jst.skim .choo input type="text" a href="#" | Do it
gem ‘skim’ Sunday, February 24, 13
EXAMPLE spec/javascripts/application_spec.js.coffee #= require spec_helper describe 'Application', -> it 'works!',
-> ($ '#konacha').html JST['templates/choo']() do ($ '.choo a').click expect($ '.choo input').to.have.value 'CHOO CHOO!11' ??? Sunday, February 24, 13
Sunday, February 24, 13
chai-jquery gem ‘chai-jquery-rails’ Sunday, February 24, 13
CHAI-JQUERY MATCHERS • .attr(name[, value]) • .data(name[, value]) • .class(className)
• .id(id) • .html(html) • .text(text) • .value(value) • .visible • .hidden • .selected • .checked • .disabled • .exist • .match(selector) / .be(selector) • .contain(selector) • .have(selector) Sunday, February 24, 13
EXAMPLE app/assets/javascripts/application.js.coffee #= require jquery jQuery -> ($ document).delegate 'a',
'click', (e) -> do e.preventDefault if confirm 'Are you sure?' ($ '.choo input').val 'CHOO CHOO!11' Sunday, February 24, 13
Sunday, February 24, 13
Sunday, February 24, 13
sinon.js http://sinonjs.org/ Sunday, February 24, 13
SINON.JS •stubs •mocks •spies •fake timers •fake XHR •fake servers
•... Sunday, February 24, 13
spec/javascripts/spec_helper.coffee #= require_tree ./templates #= require chai-jquery #= require application
#= require support/sinon #= require_tree ./support # Needed for stubbing out "window" properties # like the confirm dialog Konacha.mochaOptions.ignoreLeaks = true beforeEach -> @sandbox = sinon.sandbox.create() afterEach -> @sandbox.restore() Sunday, February 24, 13
EXAMPLE spec/javascripts/application_spec.js.coffee #= require spec_helper describe 'Application', -> beforeEach ->
@sandbox.stub(window, "confirm").returns(true) it 'works!', -> ($ '#konacha').html JST['templates/choo']() do ($ '.choo a').click expect(($ '.choo input')).to.have.value 'CHOO CHOO!11' Sunday, February 24, 13
WHAT ABOUT AJAX REQUESTS? Sunday, February 24, 13
STUBBING AJAX RESPONSES #= require jquery jQuery -> $(document).delegate 'a',
'click', (e) -> do e.preventDefault $.get '/omg', (data) -> $('.choo input').val data Sunday, February 24, 13
STUBBING AJAX RESPONSES #= require spec_helper describe 'Application', -> beforeEach
-> @server = do sinon.fakeServer.create @server.respondWith '/omg', (xhr, params) -> xhr.respond 200, {'Content-Type': 'application/ text'}, 'CHOO CHOO!11' afterEach -> do @server.restore it 'works!', -> ($ '#konacha').html JST['templates/choo']() do ($ '.choo a').click do @server.respond expect(($ '.choo input')).to.have.value 'CHOO CHOO!11' Sunday, February 24, 13
THANK YOU twitter.com/HomerJSimpson twitter.com/dreamfa11 github.com/dreamfall Brainspec brainspec.com Sunday, February 24,
13