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
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
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
7.5k
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.3k
ふつうのFeature Flag実践入門
irof
8
4.1k
OSもどきOS
arkw
0
580
スマートグラスで並列バイブコーディング
hyshu
0
260
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.4k
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
21
7k
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
260
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.8k
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.5k
AIで効率化できた業務・日常
ochtum
0
140
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.4k
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
GitHub's CSS Performance
jonrohan
1033
470k
Abbi's Birthday
coloredviolet
3
8.2k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
210
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
170
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.6k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.9k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
580
Claude Code のすすめ
schroneko
67
230k
Scaling GitHub
holman
464
140k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
160
Raft: Consensus for Rubyists
vanstee
141
7.6k
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