FF5縛り動画も次のステージへ・・・。
気の利いたテクニックややり込みにはもう飽きた!俺たちは負けるところが見たいんだ!
Headline Reader見てて思った。
「Thunderbirdみたいな3ペイン」で「携帯のフルブラウザからも見れる」「サービス型」のフィードリーダーは無いかな?
実際のページがプレビューできるタイプのやつ。(要約しか配信しないとこがあるからね!!)
どなたか知っていたら教えてくださいー
Shouldaのテストヘルパーって、モデルの宣言とまったく同じような書き方で、身も蓋も無い。
テスト:
class PrefectureTest > ActiveSupport::TestCase
should_belong_to :region
should_have_many :graves
should_have_many :inquiries
should_have_many :request_for_publishings
should_require_attributes :name, :reading, :roman
should_require_unique_attributes :roman
end
モデル:
class Prefecture > ActiveRecord::Base
belongs_to :region
has_many :graves
has_many :inquiries
has_many :request_for_publishings
validates_presence_of :name, :reading, :roman
validates_uniqueness_of :roman
end
こんなん書いて意味あんのかな?ってちょっと思ったんですが、モデルに少し変更があってそれに気付かず、速攻でテストが通らなくなった。
あー、「強い型付けより強いテスト」ってこれのことか!?
テストがまるで静的型付け言語の型定義みたいだ。
テストを書きまくってる人にとっては、「テストがこれだけなんて心配だ」とそわそわするかもしれない。「強い型付け」なんてこんな程度のもんだ。
「強いテスト」はここからshould … do …と強記していける。
なるほどなー、と今頃思った。
![]() |
|
追記:最近ではshould have_many(:foos).throughみたいな書き方が主流っぽいです。汎用性を考えると仕方ないけど、個人的には上記の様なまるで型のように見える古い書き方のほうが好き・・・。
この間書いた、簡単テストライブラリShouldaのARマクロを(gettextの)日本語で使うプラグインshoulda_jaを下記に置いときます。
ruby script/plugin install http://svn.komagata.org/rails/plugins/shoulda_ja/
くそっ!くそう・・・・。
おれは、、、おれはだだ
このリンクをブログに張ってエスパー伊東ステッカーが欲しいだけなのに・・・
もう、MTがむずすぎる・・・ウィジェットとかウィジェットセットとか・・・よくわかんない・・・。
簡単テストライブラリShouldaを使うと、RailsのModelのテストが楽だ。
class RegionTest < ActiveSupport::TestCase
should_require_attributes :name
should_require_unique_attributes :roman
end
豊富なマクロのおかげでこんな感じに宣言的にテストが書ける。
でも単体でテストを実行したとき(ruby test/units/region_test.rb)はOKなのにrakeで実行すると(rake test:units)エラーになる。
33) Failure:
test: Region should require name to be set. (RegionTest)
(略)
/blank/ not found in ["名前を入力してください。"] when set to nil.
<nil> is not true.
34) Failure:
test: Region should require unique value for roman. (RegionTest)
(略)
/taken/ not found in ["ローマ字はすでに存在します。"] when set to "hokkaidou-touhoku".
<nil> is not true.
うーん・・・。
def should_require_attributes(*attributes)
message = get_options!(attributes, :message)
message ||= <strong>/blank/</strong>
(略)
end
end
ソースを見てみると、should_require_attributesメソッドはエラーメッセージを/blank/という正規表現で決め打ち比較してるみたい。(validates_presence_ofのデフォルトが”“can‘t be blank”“だから)
そういういい加減なの、嫌いじゃない。
environments読み込むとgettextがエラーメッセージを日本語化するのでエラーになっちゃうのか。
でも、
class RegionTest < ActiveSupport::TestCase
should_require_attributes :name,
:message => "名前を入力してください。"
should_require_unique_attributes :roman,
:message => "ローマ字はすでに存在します。"
end
いちいちこんな風にテスト書かなきゃいけないんでは面倒過ぎてアメリカに亡命したくなる。
プラグインより後に何かを読み込むにはどうすりゃいいんだろう?
% tree vendor/plugins/shoulda_ja
vendor/plugins/shoulda_ja
|-- init.rb
`-- lib
`-- shoulda
`-- active_record_helpers.rb
良い方法とは思えないけど、プラグインはアルファベット順に読まれるはずなのでshoulda_jaというプラグインを置いて上書きしてみた。
init.rb:
require 'shoulda'
require File.dirname(__FILE__) + '/lib/shoulda/active_record_helpers'
lib/shoulda/active_record_helpers.rb:
module ThoughtBot
module Shoulda
module ActiveRecord
def should_require_attributes(*attributes)
message = get_options!(attributes, :message)
message ||= <strong>/入力してください/</strong>
(略)
end
def should_require_unique_attributes(*attributes)
message, scope = get_options!(attributes, :message, :scoped_to)
scope = [*scope].compact
message ||= <strong>/すでに存在します/</strong>
(略)
end
end
end
end
うほ、動いた。
・・・・・・・・・アドホック!!!