41
Railsテスティング環境 2009 晩秋 2009-10-31 OSC 2009 Tokyo/Fall (株)永和システムマネジメント | Rails勉強会@東京 諸橋恭介 2009112日月曜日

Rails testing environment, 2009 fall

Embed Size (px)

DESCRIPTION

introduce rails and its testing frameworks in pen source conference 2009 Tokyo/Fall at 2009-10-31

Citation preview

Page 1: Rails testing environment, 2009 fall

Railsテスティング環境

2009 晩秋2009-10-31 OSC 2009 Tokyo/Fall(株)永和システムマネジメント | Rails勉強会@東京

諸橋恭介

2009年11月2日月曜日

Page 2: Rails testing environment, 2009 fall

(::)

2009年11月2日月曜日

Page 3: Rails testing environment, 2009 fall

Model by AR or Ruby

Controller by AC

View by Haml or ..(many)

Helper by AV or ..

2009 -

RSpec on Rails

RSpec on Rails

RSpec on Rails

RSpec on Rails

2009-01-24 仙台Ruby会議012009年11月2日月曜日

Page 4: Rails testing environment, 2009 fall

2009年のRailsテスト環境

✓Cucumberが熟してきた

✓すでに実用のRSpecも着々と地味に便利な機能が増えている

✓Railsのテストに興味ある人はRails勉強会@東京にどうぞ

2009年11月2日月曜日

Page 5: Rails testing environment, 2009 fall

(::)

2009年11月2日月曜日

Page 6: Rails testing environment, 2009 fall

諸橋恭介✓Rails勉強会@東京の案内係をしています。

✓ここ3年ほどRubyやRailsのプロジェクトで仕事してます。

✓http://ruby.agile.esm.co.jp/

http://d.hatena.ne.jp/moro/

2009年11月2日月曜日

Page 7: Rails testing environment, 2009 fall

本を書きました

http://amazon.jp/gp/product/4797336625

いっぱい売れるとCucumberの事を追加した第2版が出るかも

2009年11月2日月曜日

Page 8: Rails testing environment, 2009 fall

2009年11月2日月曜日

Page 9: Rails testing environment, 2009 fall

(::)

2009年11月2日月曜日

Page 10: Rails testing environment, 2009 fall

今やRubyはテスト環境の主戦場と化してる

角谷信太郎 「いきいきとしたレガシーコードとの暮らし」 2008-10 札幌Ruby会議01

2009年11月2日月曜日

Page 11: Rails testing environment, 2009 fall

Cucumber&RSpec

✓受け入れテストをCucumberで

✓DeveloperテストをRSpecで

2009年11月2日月曜日

Page 12: Rails testing environment, 2009 fall

(::)

2009年11月2日月曜日

Page 13: Rails testing environment, 2009 fall

http://cukes.info2009年11月2日月曜日

Page 14: Rails testing environment, 2009 fall

Cucumber

✓自然言語で仕様を記述し、実行するための受け入れテストフレームワーク

✓ユーザ視点で、アプリケーションを

ブラックボックステストする

2009年11月2日月曜日

Page 15: Rails testing environment, 2009 fall

フィーチャ: プロジェクト特化言語を作りたい 開発者として、 お客様とのコミュニケーションを充実させるため プロジェクトに特化した言語が欲しい

シナリオ: アプリケーション上の操作を同じ言葉で話す もし "'hoge'リンクをクリックする"という表現がある ならば お客様は、リンクをクリックして遷移すると理解できること かつ 開発者は、アンカーテキストがhogeなa要素のhrefに遷移すると理解できること

シナリオ: 画面要素の呼び方を統一したい もし "'プロジェクト情報ヘッダ'部分"という表現がある ならば お客様は、画面左斜め上のプロジェクト情報を表示している箇所だと理解できること かつ 開発者は、"div#content div.project div.header"要素であると理解できること

自然言語で仕様を記述

2009年11月2日月曜日

Page 16: Rails testing environment, 2009 fall

Execute Plain Text

✓ “features”✓ 実行したい仕様をプレーンテキストで記述する

✓ “step_definitions”✓ プレーンテキストのfeaturesに対応するRubyコードを探し、実行する

2009年11月2日月曜日

Page 17: Rails testing environment, 2009 fall

フィーチャ: プロジェクト特化言語を作りたい 開発者として、 お客様とのコミュニケーションを充実させるため プロジェクトに特化した言語が欲しい

シナリオ: アプリケーション上の操作を同じ言葉で話す もし "'hoge'リンクをクリックする"という表現がある ならば お客様は、リンクをクリックして遷移すると理解 かつ 開発者は、アンカーテキストがhogeなa要素のhre

features

2009年11月2日月曜日

Page 18: Rails testing environment, 2009 fall

Given( /^Wiki"(.*)"のトップページ"(.*)"を作成する$/) do |nname, pname|  note = Note.find_by_name(nname)  attrs = valid_attributes[:page].merge({ :display_name => pname })  page = note.build_front_page  page.attributes = attrs  content = "#{nname} のトップページへようこそ!"  page.edit(content, @user)  page.saveend 

http://github.com/openskip/skip-wiki/blob/master/features/step_definitions/skip_note_steps.rb

step_definitions

2009年11月2日月曜日

Page 19: Rails testing environment, 2009 fall

# Order is opposite from original (English) versionWhen /^"([^\"]*)"の"([^\"]*)"リンクをクリックする$/ do |parent, link|  When %Q(I follow "#{link}" within "#{parent}")end When /^"([^\"]*)"に"([^\"]*)"と入力する$/ do |field, value|  When %Q(I fill in "#{field}" with "#{value}")end

http://github.com/moro/miso/blob/master/rails_generators/miso/templates/webrat_ja_steps.rb

step_definitions

2009年11月2日月曜日

Page 20: Rails testing environment, 2009 fall

Black box

•Railsのモデル・ビュー・コントローラをすべてつなげてテストできる

•:mechanizeモードで動かせば別サーバをスクレイピングでテストする

•フレームワークや言語に依存しない

2009年11月2日月曜日

Page 21: Rails testing environment, 2009 fall

Rails勉強会@東京にて

✓参加者がいろんなところでやったCucumberプレゼンの再演

✓使ってみたいけど、、、悩み相談✓Cucumberハンズオン

2009年11月2日月曜日

Page 22: Rails testing environment, 2009 fall

(::)

2009年11月2日月曜日

Page 23: Rails testing environment, 2009 fall

http://rspec.info2009年11月2日月曜日

Page 24: Rails testing environment, 2009 fall

describe DeltaAttack::FiletypeAssumption do  include SpecHelper  it "should not support_magic" do    DeltaAttack::FiletypeAssumption.should_not be_support_magic  end   describe "new('hoge.xls')" do    before do      @asm = DeltaAttack::FiletypeAssumption.new('hoge.xls')    end     it "filetype.should == :excel" do      @asm.filetype.should == :excel    end  endend

http://github.com/moro/delta_attack/blob/master/spec/filetype_assumption_spec.rb

2009年11月2日月曜日

Page 25: Rails testing environment, 2009 fall

RSpec

✓デベロッパーテストを記述するためのテスティングフレームワーク

✓ホワイトボックステストで開発を駆動する

✓もうすっかりメジャーになった (?)

2009年11月2日月曜日

Page 26: Rails testing environment, 2009 fall

じりじりと機能追加

✓subject { }

✓its(:attr){ }

2009年11月2日月曜日

Page 27: Rails testing environment, 2009 fall

subject { }✓そのExample(it)で検証する主語(=subject)

が何かを明示する。

✓it{ }のなかでレシーバを指定せずにshould

を呼ぶのに使う。

✓subject{ }ブロックの評価結果に対してshouldで検証できる。

2009年11月2日月曜日

Page 28: Rails testing environment, 2009 fall

describe Array do before do @arr = [1,2,3] end it{ @arr.should have(3).items }end

2009年11月2日月曜日

Page 29: Rails testing environment, 2009 fall

describe Array do before do @arr = [1,2,3] end

subject{ @arr } it{ should have(3).items }end

2009年11月2日月曜日

Page 30: Rails testing environment, 2009 fall

✓テストデータや状態のセットアップはbefore {} で。

✓ subject{}はあくまで、セットアップしたデータの中のとくに着目したいものを書く。

beforeとの使い分け

2009年11月2日月曜日

Page 31: Rails testing environment, 2009 fall

its()

✓最新版 1.2.9 のホヤホヤ新機能です。

✓ subject{} の評価結果に対してits()の引数をsendしたものが主語になります。

2009年11月2日月曜日

Page 32: Rails testing environment, 2009 fall

describe Array do before do @arr = [1,2,3] end

subject{ @arr.size } it{ should == 3 }end

2009年11月2日月曜日

Page 33: Rails testing environment, 2009 fall

describe Array do before do @arr = [1,2,3] end

subject{ @arr } its(:size){ should == 3 }end

2009年11月2日月曜日

Page 34: Rails testing environment, 2009 fall

✓ subjectを使いたいときにネストが深くなるのを押さえて、簡単に記述できる。

✓検証対象もハッキリしている

✓CustomMatcherも数が多いとしんどいですしね。

何が便利なの?

2009年11月2日月曜日

Page 35: Rails testing environment, 2009 fall

RSpec

✓もうすっかりproduction readyですよ。

✓地味に、便利にブラッシュアップされてます。

✓とはいえ合わない人はShouldaなど、いろんなテストツールがありますよ。

2009年11月2日月曜日

Page 36: Rails testing environment, 2009 fall

Rails勉強会@東京にて✓TDDの話をよくしてます。✓ツールの使い方、新機能、テストしやすい設計とか

✓RSpecハンズオン✓豪華ゲストが来ることも!!

2009年11月2日月曜日

Page 37: Rails testing environment, 2009 fall

(::)

2009年11月2日月曜日

Page 38: Rails testing environment, 2009 fall

2009年のRailsテスト環境

✓Cucumberが熟してきた

✓すでに実用のRSpecも着々と地味に便利な機能が増えている

✓Railsのテストに興味ある人はRails勉強会@東京にどうぞ

2009年11月2日月曜日

Page 39: Rails testing environment, 2009 fall

Model by AR or Ruby

Controller by AC

View by Haml or ..(many)

Helper by AV or ..

2009 -

RSpec on Rails

RSpec on Rails

RSpec on Rails

RSpec on Rails

2009-01-24 仙台Ruby会議012009年11月2日月曜日

Page 40: Rails testing environment, 2009 fall

http://wiki.fdiary.net/rails/?RailsMeetingTokyo-00452009年11月2日月曜日

Page 41: Rails testing environment, 2009 fall

ご清聴ありがとうございました

2009年11月2日月曜日