blog.sowatchasayin

Rails etc.

Showing posts with label Rspec. Show all posts
Showing posts with label Rspec. Show all posts

autotestよりしなやかな自動テスト

autotest/autospecはとても便利。ちゃんとテストを書いてればね。
でもちょっと高圧的というか、もうちょっとゆるくやって欲しいというか。
もっとやさしい、しかも賢いの、ありました。

watchr
Flexible Continuous Testing

・コントローラとモデルだけテストを書く
・autotestは変更部分のみ流したい
と思っていたので、こうやってやりました。これをアプリケーションのrootに置いておけば、コントローラ、モデル、rspecを変更するや否や対応するテストが流れてくれます。かしこいね。

やさしいautospec.rb

watch('spec\/.*\/*.rb') {|md|system "script/spec #{md[0]} -cfs"}
['controllers', 'models'].each do |t| watch("app/#{t}/.*\.rb"){|md|
spec = md[0].gsub(/app\/#{t}\//, "").gsub(/\.rb/,"_spec.rb")
system "script/spec spec/#{t}/#{spec} -cfs"
}
end

メールもrspec on Railsしちゃおう

できるぞ~。

送るメールはファイルを直接読みました。
before do
  raw_email = File.read(RAILS_ROOT + '/どこか/test.eml')
  @mail = TMail::Mail.parse(raw_email)
  @mail.to = "アドレス"
end 

受信はコンソールからやる時と同じ。
to_sでTmailのメールオブジェクトから生メールに戻してあげて。
UserMailer.receive(@mail.to_s)

何を送ったかも~ わかるよ~♪
describe "送ったら" do
  it "送った個数が増えるはず!" do 
  lambda{
    #ここで送る
  }.should change(sent, :count).by(1)
end 
sent.first.bodyで本文見れます。

例によって受け売りです。
RSpec: Testing Mail Delivery (simple)

最新のRailsでrestful_authenticationを使う苦労に苛まれる初秋の一日でした。

2.3.4でrestful_authenticationのspecが通らない。この子たちのせいだった。

Rspecが新しいとダメな所がありました。下記のパッチみたいに直せばOKでした(update for rspec > 1.1.12らしい)。
http://github.com/dchelimsky/restful-authentication/commit/ad92c6cb52500c37ef81e30ac4e79adffc2f4d39

formatted_* routesが無くなった。これで通るっす。
- formatted_user_path(:id => '1', :format => 'xml').should == "/users/1.xml"
- formatted_user_path(:id => '1', :format => 'json').should == "/users/1.json"
+ user_path(:id => '1', :format => 'xml').should == "/users/1.xml"
+ user_path(:id => '1', :format => 'json').should == "/users/1.json"

いきさつ
What's New in Edge Rails: No More Formatted Routes
Add optional :format argument to named routes

Author

Fujimura Daisuke
http://fujimuradaisuke.com

Labels