Skip to content

Instantly share code, notes, and snippets.

@djforth
Created March 1, 2013 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djforth/5063530 to your computer and use it in GitHub Desktop.
Save djforth/5063530 to your computer and use it in GitHub Desktop.
Shared Examples for controllers
shared_examples_for 'check templete return' do |property, path|
it "should set the #{property} variable" do
assigns[property.to_sym].should_not be_nil
end
it "should render template #{path}" do
response.should render_template(path)
end
end
shared_examples_for 'check show templete return' do |property, path|
it "should set the #{property} variable" do
assigns[property.to_sym].should_not be_nil
end
it "should render template #{path}" do
response.should render_template(path)
end
end
shared_examples_for 'save successfully' do |path, notice|
it "sets a flash[:notice] " do
#pending("Flash notice")
flash[:notice].should eq(notice)
end
it "redirects to #{path}" do
# pending("redirect")
response.should redirect_to(path)
end
end
shared_examples_for 'save successfully show' do |path, notice|
it "sets a flash[:notice] " do
#pending("Flash notice")
flash[:notice].should eq(notice)
end
it "redirects to #{path}" do
# pending("redirect")
uri = (@record.slug.present?) ? @record.slug : @record.id
response.should redirect_to(path+"/#{uri}")
end
end
shared_examples_for 'save fails' do
it "returns errors" do
#pending("Flash notice")
@record.errors.should_not be_nil
end
it "return to form" do
#pending("redirect")
response.should render_template(:action => "new")
end
end
shared_examples_for 'delete item' do
it "delete the asset" do
#language = FactoryGirl.create(:project)
model.should_receive(:find).and_return(@record)
@record.should_receive(:destroy)
delete :destroy, @params
end
end
shared_examples_for 'create a new element' do |params|
it "creates a new item" do
model.should_receive(:new).with(params).and_return(@record)
post :create, @params
end
it "saves the asset" do
#pending("Save")
model.stub(:new).and_return(@record)
@record.should_receive(:save)
post :create
end
end
shared_examples_for 'edit content' do |property, path|
it 'should set the prompt variable' do
assigns[property.to_sym].should_not be_nil
assigns(property.to_sym).should == @record
end
it "should render the template #{path}" do
response.should render_template(path)
end
end
shared_examples_for 'updates from controller' do |property, data, path|
it "recieve update" do
model.should_receive(:find).and_return(@record)
# @record.should_receive(:save).and_return(true)
@record.should_receive(:update_attributes).with(data)
post :update, @params
end
it 'should set a flash notice' do
flash[:notice].should_not be_nil
end
it "should redirect to #{property} home" do
response.should redirect_to(path)
end
it 'should respond if fail' do
model.stub(:save).and_return(false)
post :update, {:id=>@record.id}
response.should render_template(:action=> "edit")
end
end
shared_examples_for 'updates from controller show' do |property, data, path|
context 'Saves successfully' do
it "recieve update" do
model.should_receive(:find).and_return(@record)
# @record.should_receive(:save).and_return(true)
@record.should_receive(:update_attributes).with(data)
post :update, @params
end
it 'should set a flash notice' do
flash[:notice].should_not be_nil
end
it "should redirect to #{property} home" do
updated = model.find(@record.id)
url = (updated.slug.present?) ? updated.slug : @record.id
response.should redirect_to(path+"/#{url}")
end
end
context 'Save fails' do
it 'should respond if fail' do
model.stub(:save).and_return(false)
post :update, {:id=>@record.id}
response.should render_template(:action=> "edit")
end
end
end
shared_examples_for 'check ajax filter' do |property|
it "should be successful" do
response.should be_success
end
it "should return list" do
assigns[property.to_sym].should_not be_nil
assigns[property.to_sym].should eq(item)
# assigns[property.to_sym].length == 6666;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment