RSpec で transaction のテスト

Posted by okkez Sun, 03 Aug 2008 15:36:00 GMT

Yuya さんのヒントをもとにしてこんな風にしてみました。

sqlite3? は spec_helper に定義した。ヘルパーメソッド。 SQLite3 だとトランザクションの入れ子が出来なかった(例外が発生する)のでこうしてます。

一応 shouldnotreceive で特定の処理から先には進んでないことは確認しているし、 エラーメッセージも確認しているのでこれでテストとしては良さそう。

あとは、実装コードの最初の方で呼んでる処理をモック化すればいいと思う。

これをモック化すると、通しのテストはどうすればいいのだろう、というのが今の疑問。

describe BatchController do describe "POST create" do describe "by login user" do describe "when posted data was invalid (subject)" do before do Question.should_not_receive(:create!) Choice.should_not_receive(:create!) unless sqlite3? ActiveRecord::Base.connection.should_receive(:rollback_db_transaction) end end before do login_as :quentin post :create, :batch => { :body => INVALID_DATA_WITHOUT_TITLE } end it "should have error message" do flash[:error].should == invalid data. end it "should redirect to ‘subject/index’" do response.should redirect_to(subjects_path) end end end end end

Posted in , | コメントはありません | タグ | atom

Trackbacks

Use the following link to trackback from your own site:
http://typo.okkez.net/trackbacks?article_id=rspec-%E3%81%A7-transaction-%E3%81%AE%E3%83%86%E3%82%B9%E3%83%88&day=04&month=08&year=2008

Comments

Leave a response

Leave a comment