簡単なsinatraアプリを例としたテスト駆動開発の紹介をします。今回作成するsinatraアプリの完成コードは以下です。
rspec-expectations ships with a number of built-in matchers. Each matcher can be used with expect(..).to or expect(..).not_to to define positive and negative expectations respectively on an object. Most matchers can also be accessed using the (...).should and (...).should_not syntax; see using should syntax for why we recommend using expect. e.g. expect(result).to eq(3) expect(list).not_to be_empt
■テストの際にも使える(例:ファイルアップのテスト) ファイルアップは、multipartフォームが用いられ、malutipart/form-dataで扱われるオブジェクト は、StringIO(大きいとFileになる?)であり、かつ、以下の3つのメソを持っている。 - ・local_path ・original_filename ・content_type - しかし、テストでFileオブジェクトを生成して、 post :upload :file => File.open(filename)のようにやっても、例えば、コントローラの方で、 params[:file].original_filenameとかの操作を行っていたらならば、当然、UndefMethodエラー となる。 これを防止するために、IOを持つFileクラスのオブジェクトに、上記の3つのメソッドを追加して、 あたかもmult
Myron Marston » Notable Changes in RSpec 3の雑な訳です。 誤訳・雑すぎる訳がありましたら、Twitterで@nilp_までご連絡頂けると助かります。 RSpec 3.0.0 RC1が2日前にリリースされました、そして最終的な3.0.0のリリースが目前に迫っています。 我々はβ版をここ6ヶ月にわたり使ってきました、我々はそれらを皆さんと共有できることにわくわくしています。 これが新しいとこだよ: すべてのgemたちにわたって Ruby 1.8.6と1.9.1のサポートがなくなりました これらのバージョンのRubyはかなり前に寿命を迎えました、RSpecはこれらをサポートしません。 Ruby 2.xのサポート向上 最近のRSpec 2.xのリリース(すなわち2.0がリリースされたあと出たやつ)はRuby 2を公式にサポートしています、しかしRSpec
Myron MarstonJul 15, 2013Update: There’s a Japanese translation of this available now. RSpec 2.0 was released in October 2010. In the nearly three years since then, we’ve been able to continually improve RSpec without needing to make breaking changes, but we’ve reached a point where RSpec has a fair bit of cruft stemming from the need to retain backwards compatibility with older 2.x releases. RSpe
We've developed a new syntax for message expectations in RSpec, which brings message expectations in line with the new expect syntax for state based expectations. This change required us to make a number of internal refactorings, so we're cutting a release candidate of RSpec, 2.14rc1, so that early adopters can try this new feature out. We encourage you to try out the release candidate and report
Matcherの定義はここ。beに引数があればBeSameAs#matches?がshouldによって呼ばれる仕組みになっている。 整理して実際に試してみよう。以下のようにString, Array, Fixnumの型のインスタンスについてそれぞれeq, eql, equal, beのマッチャでSpecを実施してみた。 # ./tmp_spec.rb require 'rspec' describe 'eq' do it('should find two strings are equal') { "".should eq ""} it('should find two Arrays are equal') { {:a => 1}.should eq :a => 1 } it('should find two Fixnums are equal') { 1.should eq 1 } en
TDD, BDD にはテストを実行する速度がすごい重要なのはみんな知っていると思うけど、 it { … } 記法が速度にどう影響するか知ってる? という話。 場合によっては、 subject + it, its で何行も並べるより、まとめられるなら 1 つの example にまとめたほうが速いです。 # May be slow describe 'foo' do subject { get :foo } before do # Prepare the model (e.g. FactoryGirl) end it { should be_ok } its(:body) { should match(/foo/) } # ... after do # Clear the DB (e.g. DatabaseCleaner) end end 前提 before や let! でたくさんモデルを
来年も作りたい!ふきのとう料理を満喫した 2024年春の記録 春は自炊が楽しい季節 1年の中で最も自炊が楽しい季節は春だと思う。スーパーの棚にやわらかな色合いの野菜が並ぶと自然とこころが弾む。 中でもときめくのは山菜だ。早いと2月下旬ごろから並び始めるそれは、タラの芽、ふきのとうと続き、桜の頃にはうるい、ウド、こ…
最近sinatraを使い始めました。 rspecをsinatraで使い始めるまで右往左往したのでその時のメモ cucumberも使おうかと思ったけど、あれはお客さんに見せる事ができるテスト仕様だから美味しいんであって、個人の趣味プログラミングで、しかもsinatra使ってサックリとサイト作るには手軽さが損なわれる気がしたのでとりあえず保留。 sinatraをインストール $ sudo gem install sinatra $ sudo gem install rack appを作成 $ mkdir myapp $ cd myapp $ touch app.rb /myapp/app.rb require "rubygems" require "sinatra" get '/' do "hello" end get '/hoge' do "get params #{params[:huga
module Foo module Helpers def bar 'unk' end end end こういうHelpersを helpers do include Foo::Helpers end こういう風に使ってた場合。 RSpec.configure do |config| config.include Foo::Helpers end describe Foo::Helpers do context 'bar' do it 'should return unk' do bar.should eql('unk') end end end Spec::Runner.configでincludeするとテスト出来る。 LokkaはSinatraベースなので同じようにHelpersのテスト書ける。でもRspecややこしいな。config.includeのとことか。とTest::Unit,
■ はじまり 最近携わっている Rails 3 アプリの自動テスト(RSpec)が、すべて完了するのに1時間半かかるようになってしまいました。 5〜10分で終わっていた頃はファイルに変更を加えるたびにすべてのテストを確認できていましたが、それが難しくなってきたので Guard を導入することにしました。 Guard を利用すれば、変更のあったファイルに関連するのみを効率よくテストすることができます。 ローカルでは Gurad で自分が編集した部分のみテストして、リモートの jenkins などですべてのテストを確認、ということをできるようにするのが今回の目的です。 ■ Guard のインストール というわけで、さっそく Rails 3 アプリに Guard を導入していきます。 bundlerを利用している場合は Gemfile ファイルに下記の行を追加します。 group :develo
というように書くようになりました。 別にshouldを使った記法がなくなったわけではありませんが、 https://github.com/rspec/rspec-expectations のREADME.mdには、もう新しいSyntaxの説明しか載っていないし、今後はexpectの方を使っていくほうがいいでしょう。 http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax には、新しいSyntaxを導入した背景が説明されています。 簡潔に書くと、shouldだとBasicObjectを継承したクラスのテストを書くときに不具合が起こるみたいですね。 移行方法 基本 基本的には、上に書いたように、 foo.should を expect(foo).to に foo.should_not を expect(foo).
というように書くようになりました。 別にshouldを使った記法がなくなったわけではありませんが、 https://github.com/rspec/rspec-expectations のREADME.mdには、もう新しいSyntaxの説明しか載っていないし、今後はexpectの方を使っていくほうがいいでしょう。 http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax には、新しいSyntaxを導入した背景が説明されています。 簡潔に書くと、shouldだとBasicObjectを継承したクラスのテストを書くときに不具合が起こるみたいですね。 移行方法 基本 基本的には、上に書いたように、 foo.should を expect(foo).to に foo.should_not を expect(foo).
Ruby 2.0.0 p0の話かと思った?残念RSpec 2.13.0ちゃんでした! Changelogを見てたらテンション上がってきたので使ってみました。 subject! let!のsubject版 通常のsubjectが遅延評価でshouldが呼ばれた時に評価されるのに対し、subject!はbefore(:each)でhookして実行される模様。 戻り値以外で評価するメソッドのテストでitやbefore(:each)に書いていたものは subject! に移動させるとよさげ --profile テストの実行時間を計測してくれるのでトップ10を出してくれるスローテストの調査に重宝しそう。 プロファイル結果をJenkinsで集計するプラグインが望まれる。 $ be rspec --profile .............................................
gems ディレクトリの */spec/**/*_spec.rb ファイルに対して 'it .* do' を grep する。 cd ~/.rvm/gems/ruby-1.9.3-p327/gems find */spec -type f -name '*_spec.rb' | xargs grep -ho 'it .* do' | sort | uniq | lessみたいな感じ。以下のような出力が得られる。 : it "accepts a URL as the path" do it "accepts a block to change output" do it "accepts a block" do it "accepts a body" do it "accepts a class as argument with a task to invoke" do it "accept
Confused by #stub vs. #stub!? Consider #stub! deprecated and use #stub. #stub is simply an alias for #stub!. #stub! was defined in the API at a time when RSpec's authors weren't clear on the intention of "bang" methods in Ruby. Quoting David Chelimsky's email on RubyForge: The original method was stub!, which I will confess to introducing, and for the wrong reasons, due to my misunderstanding of t
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く