Skip to content

Instantly share code, notes, and snippets.

@neophiliac
Forked from cassiomarques/gist:82345
Created November 15, 2010 16:03
Show Gist options
  • Save neophiliac/700509 to your computer and use it in GitHub Desktop.
Save neophiliac/700509 to your computer and use it in GitHub Desktop.
def extract_model_name(controller_class); controller_class.to_s.match(/(.+)Controller/)[1]; end
def model(controller_class); extract_model_name(controller_class).singularize.constantize; end
def symbol_for_model(controller_class, options = {})
tablename = extract_model_name(controller_class).tableize
options[:pluralize] ? tablename.to_sym : tablename.singularize.to_sym
end
describe "an ordinary 'show' action", :shared => true do
before do
model(described_class).stub!(:find).and_return(@object = mock_model(model(described_class)))
end
def do_get_request; get :show, :id => "1"; end
it "should be successfull" do
do_get_request
response.should be_success
end
it "should render the 'show' template" do
do_get_request
response.should render_template('show')
end
it "should find the object to display" do
model(described_class).should_receive(:find).with("1").and_return(mock_model(model(described_class)))
do_get_request
end
it "should assign the found object to the view" do
do_get_request
assigns[symbol_for_model(described_class)].should eql(@object)
end
end
describe "an ordinary 'index' action", :shared => true do
before do
model(described_class).stub!(:all).and_return(@list = [mock_model(model(described_class))])
end
def do_get_request; get :index; end
it "should render the 'index' template" do
do_get_request
response.should render_template('index')
end
it "should be successfull" do
do_get_request
response.should be_success
end
it "should paginate the found objects" do
model(described_class).should_receive(:all).and_return([mock_model(model(described_class))])
do_get_request
end
it "should assign a list of objects to the view" do
do_get_request
assigns[symbol_for_model(described_class, :pluralize => true)].should eql(@list)
end
end
describe "an ordinary 'new' action", :shared => true do
before do
model(described_class).stub!(:new).and_return(@object = mock_model(model(described_class)))
end
def do_get_request; get :new; end
it "should render the 'new' template" do
do_get_request
response.should render_template('new');
end
it "should be sucessful" do
do_get_request
response.should be_success
end
it "should assign the new object to the view" do
do_get_request
assigns[symbol_for_model(described_class)].should eql(@object)
end
it "should create a new object" do
model(described_class).should_receive(:new).and_return(mock_model(model(described_class)))
do_get_request
end
end
describe "an ordinary 'edit' action", :shared => true do
before do
model(described_class).stub!(:find).and_return(@object = mock_model(model(described_class)))
end
def do_get_request; get :edit, :id => "1"; end
it "should be successful" do
do_get_request
response.should be_success
end
it "should render the 'edit' template" do
do_get_request
response.should render_template('edit')
end
it "should find the object for edition" do
model(described_class).should_receive(:find).with("1").and_return(mock_model(model(described_class)))
do_get_request
end
it "should assign the found object to the view" do
do_get_request
assigns[symbol_for_model(described_class)].should eql(@object)
end
end
describe "an ordinary 'create' action", :shared => true do
def do_post_request
post :create, symbol_for_model(described_class) => {"foo" => "bar"}
end
describe "with valid params" do
before do
model(described_class).stub!(:new).and_return(@object = mock_model(model(described_class), :save => true))
end
it "should create a new object" do
@mock = mock_model(model(described_class))
@mock.should_receive(:save).and_return(true)
model(described_class).should_receive(:new).with("foo" => "bar").and_return(@mock)
do_post_request
end
it "should assign the newly created object to the view" do
do_post_request
assigns[symbol_for_model(described_class)].should eql(@object)
end
it "should redirect to the index page" do
do_post_request
response.should redirect_to(eval("#{symbol_for_model(described_class, :pluralize => true).to_s}_url"))
end
it "should have a message telling that the object was successfully created" do
do_post_request
flash[:info].should match(/(.+) criad[oa] com sucesso!/)
end
end
describe "with invalid params" do
before do
model(described_class).stub!(:new).and_return(@object = mock_model(model(described_class), :save => false))
end
it "should assign a newly created but unsaved object to the view" do
do_post_request
assigns[symbol_for_model(described_class)].should eql(@object)
end
it "should re-render the 'new' template" do
do_post_request
response.should render_template('new')
end
it "should have a message telling that the object could not be created" do
do_post_request
flash[:error].should match(/Não foi possível criar [ao] (.+)/)
end
end
end
describe "an ordinary 'update' action", :shared => true do
def do_put_request
put :update, :id => "1", symbol_for_model(described_class) => {:foo => "bar"}
end
describe "with valid params" do
before(:each) do
model(described_class).stub!(:find).with("1").and_return(@object = mock_model(model(described_class), :update_attributes => true))
end
it "should expose the requested object to the view" do
do_put_request
assigns[symbol_for_model(described_class)].should eql(@object)
end
it "should redirect to index page" do
do_put_request
response.should redirect_to(eval("#{symbol_for_model(described_class, :pluralize => true).to_s}_url"))
end
it "should have a message telling that the resource was updated" do
do_put_request
flash[:info].should match(/(.+) atualizad[oa] com sucesso!/)
end
end
describe "with invalid params" do
before(:each) do
model(described_class).stub!(:find).with("1").and_return(@object = mock_model(model(described_class), :update_attributes => false))
end
it "should expose the requested object to the view" do
do_put_request
assigns[symbol_for_model(described_class)].should eql(@object)
end
it "should re-render the 'edit' template" do
do_put_request
response.should render_template('edit')
end
it "should have a message telling that the equipment could not be updated" do
do_put_request
flash[:error].should match(/Não foi possível atualizar [oa] (.+)/)
end
end
end
describe "an ordinary 'destroy' action", :shared => true do
before(:each) do
model(described_class).stub!(:find).and_return(mock_model(model(described_class),:destroy => true))
end
it "should destroy the requested resource" do
model(described_class).should_receive(:find).with("1").and_return(@object = mock_model(model(described_class)))
@object.should_receive(:destroy).and_return(true)
delete :destroy, :id => "1"
end
it "should redirect to the index page" do
delete :destroy, :id => "1"
response.should redirect_to(eval("#{symbol_for_model(described_class, :pluralize => true).to_s}_url"))
end
it "should have a message telling that the resource was succesfully destroyed" do
delete :destroy, :id => "1"
flash[:info].should match(/(.+) apagado com sucesso\./)
end
describe "with failure" do
before do
mock = mock_model(model(described_class))
mock.stub!(:destroy).and_raise(Exception)
model(described_class).stub!(:find).and_return(mock)
end
it "should redirect to the index page" do
delete :destroy, :id => "1"
response.should redirect_to(eval("#{symbol_for_model(described_class, :pluralize => true).to_s}_url"))
end
it "should have a message telling that the resource could not be deleted" do
delete :destroy, :id => "1"
flash[:error].should match(/Não foi possível apagar [ao] .+/)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment