Skip to content

Instantly share code, notes, and snippets.

@josevalim
Created April 13, 2009 20:45
Show Gist options
  • Save josevalim/94700 to your computer and use it in GitHub Desktop.
Save josevalim/94700 to your computer and use it in GitHub Desktop.
Comparision of Remarkable way with Rspec way of testing controllers
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe TasksController do
mock_models :task
describe :get => :index do
expects :find, :on => Task, :with => :all, :returns => mock_task
should_assign_to :tasks, :with => mock_task
describe Mime::XML do
expects :to_xml, :on => mock_task, :returns => 'generated xml'
should_assign_to :tasks, :with => mock_task
should_respond_with :body => /generated_xml/, :content_type => Mime::XML
end
end
describe :get => :show, :id => '37' do
expects :find, :on => Task, :with => '37', :returns => mock_task
should_assign_to :task, :with => mock_task
describe Mime::XML do
expects :to_xml, :on => mock_task, :returns => 'generated xml'
should_assign_to :task, :with => mock_task
should_respond_with :body => /generated_xml/, :content_type => Mime::XML
end
end
describe :get => :new do
expects :new, :on => Task, :returns => mock_task
should_assign_to :task, :with => mock_task
end
describe :get => :edit, :id => '37' do
expects :find, :on => Task, :with => '37', :returns => mock_task
should_assign_to :task, :with => mock_task
end
describe :post => :create, :task => {:these => 'params'} do
expects :new, :on => Task, :with => {'these' => 'params'}, :returns => mock_task
describe "with valid params" do
expects :save, :on => mock_task, :returns => true
should_assign_to :task, :with => mock_task
should_redirect_to { task_url(mock_task) }
end
describe "with invalid params" do
expects :save, :on => mock_task, :returns => false
should_assign_to :task, :with => mock_task
should_render_template 'new'
end
end
describe :put => :update, :id => "37", :task => {:these => 'params'} do
expects :find, :on => Task, :with => '37', :returns => mock_task
describe "with valid params" do
expects :update_attributes, :on => mock_task, :with => {'these' => 'params'}, :returns => true
should_assign_to :task, :with => mock_task
should_redirect_to { task_url(mock_task) }
end
describe "with invalid params" do
expects :update_attributes, :on => mock_task, :with => {'these' => 'params'}, :returns => false
should_assign_to :task, :with => mock_task
should_render_template 'edit'
end
end
describe :delete => :destroy do
expects :find, :on => Task, :with => '37', :returns => mock_task
expects :destroy, :on => mock_task
should_assign_to :task, :with => mock_task
should_redirect_to { tasks_url }
end
end
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe TasksController do
def mock_task(stubs={})
@mock_task ||= mock_model(Task, stubs)
end
describe "GET index" do
it "exposes all tasks as @tasks" do
Task.should_receive(:find).with(:all).and_return([mock_task])
get :index
assigns[:tasks].should == [mock_task]
end
describe "with mime type of xml" do
it "renders all tasks as xml" do
Task.should_receive(:find).with(:all).and_return(tasks = mock("Array of Tasks"))
tasks.should_receive(:to_xml).and_return("generated XML")
get :index, :format => 'xml'
response.body.should == "generated XML"
end
end
end
describe "GET show" do
it "exposes the requested task as @task" do
Task.should_receive(:find).with("37").and_return(mock_task)
get :show, :id => "37"
assigns[:task].should equal(mock_task)
end
describe "with mime type of xml" do
it "renders the requested task as xml" do
Task.should_receive(:find).with("37").and_return(mock_task)
mock_task.should_receive(:to_xml).and_return("generated XML")
get :show, :id => "37", :format => 'xml'
response.body.should == "generated XML"
end
end
end
describe "GET new" do
it "exposes a new task as @task" do
Task.should_receive(:new).and_return(mock_task)
get :new
assigns[:task].should equal(mock_task)
end
end
describe "GET edit" do
it "exposes the requested task as @task" do
Task.should_receive(:find).with("37").and_return(mock_task)
get :edit, :id => "37"
assigns[:task].should equal(mock_task)
end
end
describe "POST create" do
describe "with valid params" do
it "exposes a newly created task as @task" do
Task.should_receive(:new).with({'these' => 'params'}).and_return(mock_task(:save => true))
post :create, :task => {:these => 'params'}
assigns(:task).should equal(mock_task)
end
it "redirects to the created task" do
Task.stub!(:new).and_return(mock_task(:save => true))
post :create, :task => {}
response.should redirect_to(task_url(mock_task))
end
end
describe "with invalid params" do
it "exposes a newly created but unsaved task as @task" do
Task.stub!(:new).with({'these' => 'params'}).and_return(mock_task(:save => false))
post :create, :task => {:these => 'params'}
assigns(:task).should equal(mock_task)
end
it "re-renders the 'new' template" do
Task.stub!(:new).and_return(mock_task(:save => false))
post :create, :task => {}
response.should render_template('new')
end
end
end
describe "PUT udpate" do
describe "with valid params" do
it "updates the requested task" do
Task.should_receive(:find).with("37").and_return(mock_task)
mock_task.should_receive(:update_attributes).with({'these' => 'params'})
put :update, :id => "37", :task => {:these => 'params'}
end
it "exposes the requested task as @task" do
Task.stub!(:find).and_return(mock_task(:update_attributes => true))
put :update, :id => "1"
assigns(:task).should equal(mock_task)
end
it "redirects to the task" do
Task.stub!(:find).and_return(mock_task(:update_attributes => true))
put :update, :id => "1"
response.should redirect_to(task_url(mock_task))
end
end
describe "with invalid params" do
it "updates the requested task" do
Task.should_receive(:find).with("37").and_return(mock_task)
mock_task.should_receive(:update_attributes).with({'these' => 'params'})
put :update, :id => "37", :task => {:these => 'params'}
end
it "exposes the task as @task" do
Task.stub!(:find).and_return(mock_task(:update_attributes => false))
put :update, :id => "1"
assigns(:task).should equal(mock_task)
end
it "re-renders the 'edit' template" do
Task.stub!(:find).and_return(mock_task(:update_attributes => false))
put :update, :id => "1"
response.should render_template('edit')
end
end
end
describe "DELETE destroy" do
it "destroys the requested task" do
Task.should_receive(:find).with("37").and_return(mock_task)
mock_task.should_receive(:destroy)
delete :destroy, :id => "37"
end
it "redirects to the tasks list" do
Task.stub!(:find).and_return(mock_task(:destroy => true))
delete :destroy, :id => "1"
response.should redirect_to(tasks_url)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment