Skip to content

Instantly share code, notes, and snippets.

@tarvaina
Created October 7, 2008 15:34
Show Gist options
  • Save tarvaina/15299 to your computer and use it in GitHub Desktop.
Save tarvaina/15299 to your computer and use it in GitHub Desktop.
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
def mock_poem(stubs={})
@mock_poem ||= mock_model(Poem, stubs)
end
describe PoemsController do
when_it "responds to GET index" do
get :index
end
before(:each) do
@all_poems = mock("poems")
Poem.stub!(:find).with(:all).and_return(@all_poems)
end
it "should expose all poems as @poems" do
assigns[:poems].should == @all_poems
end
describe "with mime type of xml" do
before(:each) do
request.env["HTTP_ACCEPT"] = "application/xml"
@all_poems.stub!(:to_xml).and_return("generated XML")
end
it "should render all poems as xml" do
response.body.should == "generated XML"
end
end
end
describe PoemsController do
when_it "responds to GET show" do
get :show, :id => "37"
end
before(:each) do
Poem.stub!(:find).with("37").and_return(mock_poem)
end
it "should expose the requested poem as @poem" do
assigns[:poem].should equal(mock_poem)
end
describe "with mime type of xml" do
before(:each) do
request.env["HTTP_ACCEPT"] = "application/xml"
mock_poem.stub!(:to_xml).and_return("generated XML")
end
it "should render the poem as xml" do
response.body.should == "generated XML"
end
end
end
describe PoemsController do
when_it "responds to GET new" do
get :new
end
before(:each) do
Poem.stub!(:new).and_return(mock_poem)
end
it "should expose a new poem as @poem" do
assigns[:poem].should equal(mock_poem)
end
end
describe PoemsController do
when_it "responds to GET edit" do
get :edit, :id => "37"
end
before(:each) do
Poem.stub!(:find).with("37").and_return(mock_poem)
end
it "should expose the requested poem as @poem" do
assigns[:poem].should equal(mock_poem)
end
end
describe PoemsController do
when_it "responds to POST create" do
post :create, :poem => {:these => 'params'}
end
before(:each) do
Poem.stub!(:new).with({'these' => 'params'}).and_return(mock_poem)
end
it "should try to save the poem", :implicitly do
mock_poem.should_receive(:save)
end
describe "with valid params" do
before(:each) do
mock_poem.stub!(:save).and_return(true)
end
it "should expose a newly created poem as @poem" do
assigns(:poem).should equal(mock_poem)
end
it "should redirect to the created poem" do
response.should redirect_to(poem_url(mock_poem))
end
end
describe "with invalid params" do
before(:each) do
mock_poem.stub!(:save).and_return(false)
end
it "should expose a newly created but unsaved poem as @poem" do
assigns(:poem).should equal(mock_poem)
end
it "should re-render the 'new' template" do
response.should render_template('new')
end
end
end
describe PoemsController do
when_it "responds to PUT update" do
put :update, :id => "37", :poem => {:these => 'params'}
end
before(:each) do
Poem.stub!(:find).with("37").and_return(mock_poem)
end
it "should try to update the requested poem", :implicitly do
mock_poem.should_receive(:update_attributes).with({'these' => 'params'})
end
describe "with valid params" do
before(:each) do
mock_poem.stub!(:update_attributes).and_return(true)
end
it "should expose the requested poem as @poem" do
assigns(:poem).should equal(mock_poem)
end
it "should redirect to the poem" do
response.should redirect_to(poem_url(mock_poem))
end
end
describe "with invalid params" do
before(:each) do
mock_poem.stub!(:update_attributes).and_return(false)
end
it "should expose the poem as @poem" do
assigns(:poem).should equal(mock_poem)
end
it "should re-render the 'edit' template" do
response.should render_template('edit')
end
end
end
describe PoemsController do
when_it "responds to DELETE destroy" do
delete :destroy, :id => "37"
end
before(:each) do
Poem.stub!(:find).with("37").and_return(mock_poem(:destroy => true))
end
it "should destroy the requested poem", :implicitly do
mock_poem.should_receive(:destroy)
end
it "should redirect to the poems list" do
response.should redirect_to(poems_url)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment