Skip to content

Instantly share code, notes, and snippets.

@dbackeus
Created April 14, 2010 12:46
Show Gist options
  • Save dbackeus/365772 to your computer and use it in GitHub Desktop.
Save dbackeus/365772 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe MonkeysController do
def mock_monkey(stubs={})
@mock_monkey ||= mock_model(Monkey, stubs).as_null_object
end
describe "GET index" do
it "assigns all monkeys as @monkeys" do
Monkey.stub(:all) { [mock_monkey] }
get :index
assigns(:monkeys).should eq([mock_monkey])
end
end
describe "GET show" do
it "assigns the requested monkey as @monkey" do
Monkey.stub(:find).with("37") { mock_monkey }
get :show, :id => "37"
assigns(:monkey).should be(mock_monkey)
end
end
describe "GET new" do
it "assigns a new monkey as @monkey" do
Monkey.stub(:new) { mock_monkey }
get :new
assigns(:monkey).should be(mock_monkey)
end
end
describe "GET edit" do
it "assigns the requested monkey as @monkey" do
Monkey.stub(:find).with("37") { mock_monkey }
get :edit, :id => "37"
assigns(:monkey).should be(mock_monkey)
end
end
describe "POST create" do
describe "with valid params" do
it "assigns a newly created monkey as @monkey" do
Monkey.stub(:new).with({'these' => 'params'}) { mock_monkey(:save => true) }
post :create, :monkey => {'these' => 'params'}
assigns(:monkey).should be(mock_monkey)
end
it "redirects to the created monkey" do
Monkey.stub(:new) { mock_monkey(:save => true) }
post :create, :monkey => {}
response.should redirect_to(monkey_url(mock_monkey))
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved monkey as @monkey" do
Monkey.stub(:new).with({'these' => 'params'}) { mock_monkey(:save => false) }
post :create, :monkey => {'these' => 'params'}
assigns(:monkey).should be(mock_monkey)
end
it "re-renders the 'new' template" do
Monkey.stub(:new) { mock_monkey(:save => false) }
post :create, :monkey => {}
response.should render_template(:new)
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested monkey" do
Monkey.should_receive(:find).with("37") { mock_monkey }
mock_monkey.should_receive(:update_attributes).with({'these' => 'params'})
put :update, :id => "37", :monkey => {'these' => 'params'}
end
it "assigns the requested monkey as @monkey" do
Monkey.stub(:find) { mock_monkey(:update_attributes => true) }
put :update, :id => "1"
assigns(:monkey).should be(mock_monkey)
end
it "redirects to the monkey" do
Monkey.stub(:find) { mock_monkey(:update_attributes => true) }
put :update, :id => "1"
response.should redirect_to(monkey_url(mock_monkey))
end
end
describe "with invalid params" do
it "assigns the monkey as @monkey" do
Monkey.stub(:find) { mock_monkey(:update_attributes => false) }
put :update, :id => "1"
assigns(:monkey).should be(mock_monkey)
end
it "re-renders the 'edit' template" do
Monkey.stub(:find) { mock_monkey(:update_attributes => false) }
put :update, :id => "1"
response.should render_template(:edit)
end
end
end
describe "DELETE destroy" do
it "destroys the requested monkey" do
Monkey.should_receive(:find).with("37") { mock_monkey }
mock_monkey.should_receive(:destroy)
delete :destroy, :id => "37"
end
it "redirects to the monkeys list" do
Monkey.stub(:find) { mock_monkey(:destroy => true) }
delete :destroy, :id => "1"
response.should redirect_to(monkeys_url)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment