I want to test a file upload in rails, but am not sure how to do this. Here is the controller code: def uploadLicense #Create the license object @license = License.create(params[:license]) #Get Session ID sessid = session[:session_id] puts "\n\nSession_id:\n#{sessid}\n" #Generate a random string chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a newpass = "" 1.upto(5) { |i| newpass << cha
ちょっと前に話題になったRSpecのスライドがステキだったよね。でもRSpecはまだまだ底知れない気がするので自分でもいろいろと調べてみようと思った次第。 まずはrspec-core(2.5.1)/features/example_groups/shared_example_group.featureを参考にshared example groupについて調べてみたよ。 例1:テストを共有できる require "set" shared_examples_for 'a collection' do subject { described_class.new [7, 2, 4] } its(:size) { should eq 3 } it { should include 7 } it { should_not include 9 } end describe Array do it_be
Similar to the problem described here: http://rpheath.com/posts/411-how-to-use-factory-girl-with-rspec in Short (shorten'd code): spec_helper: config.use_transactional_fixtures = true config.use_instantiated_fixtures = false factories.rb: Factory.define :state do f.name "NY" end in my spec before(:each) do @static_model = Factory(:state) # with validate uniqueness of state name end error: duplicat
例のごとく既出だけど今回はattribute of subjectについて。rspec-core(2.5.1)/features/subject/attribute_of_subject.feature参考。「it { subject.first.size.should eq 2 }」とかするのやだよね。そんなときはitsを使えばステキになるよって話。 基本的にはシンボルか文字列で指定する describe Array do context 'when first created' do its(:size) { should eq 0 } its('size') { should eq 0 } end end % rspec -cfs attribute_of_subject_spec_1.rb Array when first created size should == 0 size
前回は具体的なWebアプリを例にして簡単なコードレビューをしました。今回からは、テストを使ったリファクタリングについて解説していきます 少し時間が空いてしまいましたが、前回は具体的なWebアプリを例にして簡単なコードレビューをしました。今回からは、そのWebアプリに対してテストを書いてリファクタリングする具体的な方法について解説していきます。 今回はまず、Ruby on Railsで人気のあるテストフレームワークの数々についてご紹介します。 最近のテストフレームワークトレンド Hamlの作者として知られるHampton Catlin氏が行った「Hampton's Third Ruby Survey, 2010」の中に、テストに関するいくつかの興味深い結果があります。好きなテストフレームワークは何ですかという質問に対する答えをグラフにすると以下の通りです。 これを見ると「ビヘイビア駆動開発(
RSpec is going through a transition with a huge refactoring for version 2. It’s currently only in beta, but if you want to get the jump on things with RSpec 2.0 there are a few changes worth knowing to ease the upgrade path. The rake tasks have changed a little. There has been a namespace change and consequently a filename change. This is what you need to get them working again. Nothing difficult
追記2(2015/09/08)ありがたいことに、未だにこの記事をブックマークしてくださる方がいらっしゃいますが、2008年に書いた記事なのでご注意下さい。内容はアップデートしていません。私自身はすでにRubyを使っていません。 追記:古い情報ですので、記事の日付とお使いのRSpecのバージョンを見比べて、参考程度にご覧ください。大部分は通用するはずですが。 Matcherをいちいち調べるのが面倒になって、公式のリファレンスマニュアルは一覧性が低いから、自分で一覧表を作った。 RSpecそのものについては、スはスペックのス 【第 1 回】 RSpec の概要と、RSpec on Rails (モデル編)などをどうぞ。そのうちRSpec on Rails版も作る予定。 名前 not((should_notで使えるかどうかという意味。)) 意味・機能 == ○ ==演算子を利用して比較する。ex
RSpecで書いているスペック(テスト)がどうも冗長になっている気がして、いいテストのリファクタリング指針はないかなと探してみたところ、RSpecのベストプラクティスをまとめているページを2つほど見つけたのでまとめておく。 (My) RSpec best practices and tips | EggsOnBread specify {}やit {}、subject {}といったショートカット記法を使う contextを'when'や'with'で始めて、メソッドの説明には'#'を使う エラーメッセージがわかりやすくなる メッセージをわかりやすいものにするためにRSpecマッチャーを使う 1つのitブロックには1つのExpectationだけを記述する describeとcontextをふんだんに使う 妥当な値、境界値、不正な値をテストする My top 7 RSpec best pra
コントローラもテストしてみる。 ページにアクセスしてサクセスが返ることと、期待するテンプレートを表示することを確認するシンプルなケース 画面にアクセスするのは get :アクション名 成功が返るのは response.should be_success テンプレートの表示判定は response.should render_template("XXXX") describe '登録画面にアクセスしたら' do before do get :add_index end it 'サクセスであること' do response.should be_success end it '登録画面を表示すること' do response.should render_template("tunes/add") end end フォームからデータを送信したケースのテスト post でアクションに対してパラメータ
Is it possible to test the use of a given layout using RSpec with Rails, for example I'd like a matcher that does the following: response.should use_layout('my_layout_name') I found a use_layout matcher when Googling but it doesn't work as neither the response or controller seem to have a layout property that matcher was looking for.
RSpec は Ruby の BDD(Behavior Driven Development). テストフレームワークの1つです。 Install RSpec % sudo gem install rspec Quickstart 下の方にあるサンプルコードのディレクトリ構成です。 lib/ ディレクトリに開発するライブラリを配置。 spec/ ディレクトリに spec(テスト) コードを配置。 sample_project/ lib/sample.rb サンプルのライブラリ spec/ sample_spec.rb sample.rb のテストプログラム spec_helper.rb すべてのテストで共通する処理を書くスクリプト ディレクトリを作成 まず、プロジェクトのディレクトリを作成して、その中に lib/ と spec/ ディレクトリを作成します。 % mkdir sample
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く