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
初めての模倣学習とVLA
natsutan
0
470
typoなんかねぇよ
raspython3
0
660
Webエンジニアなのにブラウザの仕組みがわからないので、Pythonで自作してみた
tatsuki12
4
1.7k
Swift愛好会と私(ウホーイ) / Swift Fan Club and Uhooi
uhooi
0
130
FDEとは、何者なのか?
masapyon1212
0
140
AIを上手に使っていこうとしたら越境せざるを得なくなった話 〜実践1年で見えた境界を越えなければならない理由と進め方〜 / Crossing borders with AI
tomoyakitaura
3
1k
LoopHub - ローカルで動く GitHub で、AI と共同開発
jugyo
0
420
高専、大学編入、そして未踏へ〜プロダクト開発とキャリアの歩み - Technical College, University Transfer, and On to “Mitou” / My Journey in Product Development and Career
pkmiya
0
140
「つくるAI」だけではバグは見つからない ~テストに必要な「見つけるAI」を分離させる戦略~
mfunaki
0
230
DynamoDBの基礎を振り返りながらベクトル検索機能を理解する
musan
3
270
Deep dive into the select statement (GopherCon UK)
jespino
0
170
LL言語やWebフレームワークのPostgreSQL対応 〜DBの機能がユーザーに届くまで〜
kentaroutakeda
0
140
Featured
See All Featured
Mind Mapping
helmedeiros
1
350
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
500
SEO for Brand Visibility & Recognition
aleyda
0
4.7k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
650
HDC tutorial
michielstock
2
850
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
How to Think Like a Performance Engineer
csswizardry
28
2.8k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
270
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
240
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
570
Music & Morning Musume
bryan
47
7.4k
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