コントローラーで次の機能テストを試みています。私は以下を使用しています
- RSpec2
- レール3
- ショドラ
- モカ
これがテストです
context "POST on create should be successful" do
before(:each) do
Model.any_instance.expects(:save).returns(true).once
Model.any_instance.stubs(:id).returns(1)
post :create, :model => {}
end
it { should assign_to(:model).with_kind_of(Model) }
it { should set_the_flash.to("Model was created successfully.")}
it { should redirect_to(model_path(1))}
end
テスト対象のコントローラーは
def create
@model = Model.new(params[:model])
if @model.save
flash[:success] = "Model was created successfully."
end
respond_with @model
end
唯一の失敗したテストは、返されるパスが「http://test.host/models/1」ではなく「http://test.host/models」であることを示す 3 番目のテストです。
ブラウザでアプリケーションを実行すると、正しいリダイレクトが得られます。