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
楽しそうなつよつよエンジニアと目が死んでる僕/A brilliant engineer having a blast, and dead-eyed me.
3l4l5
2
100
いまどきの Codex で開発する visionOS アプリの開発スタイルについて
karad
0
120
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.9k
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
140
【QA Test Talk Vol.8】AI-DLC による Whole Team Approach の加速
pkshadeck
PRO
0
140
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
150
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
180
数百円から始めるRuby電子工作
tarosay
0
140
React本体のコードリーディング
high_g_engineer
1
140
php-fpmのプロセスが枯渇した日-調査・対処・そして本当にやるべきだったこと-
shibuchaaaan
0
260
Cloudflare is Agents
chimame
0
160
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
210
Featured
See All Featured
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
790
Large-scale JavaScript Application Architecture
addyosmani
515
110k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.2k
WENDY [Excerpt]
tessaabrams
11
39k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.5k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
260
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
550
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
470
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
460
Product Roadmaps are Hard
iamctodd
55
12k
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