Skip to content

Commit

Permalink
added functional tests to documents
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaime Bellmyer committed Jan 6, 2010
1 parent beea5bb commit 8db751d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 12 deletions.
68 changes: 65 additions & 3 deletions test/functional/documents_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,70 @@
require 'test_helper'

def should_allow_everything_for(type, user)
context "as #{type}" do
setup do
login_as user if user
end

context "getting new" do
setup do
get :new
end

should_assign_to :document, :class => Document
should_respond_with :success
should_render_without_layout
should_not_set_the_flash
end

context "creating" do
context "with valid data" do
setup do
Document.any_instance.stubs(:save).returns(true)
post :create, :document => {}
end

should_assign_to :document, :class => Document
should_respond_with :success
should_render_without_layout
should_not_set_the_flash
end

context "with invalid data" do
setup do
Document.any_instance.stubs(:save).returns(false)
post :create, :document => {}
end

should_assign_to :document, :class => Document
should_respond_with :success
should_render_without_layout
should_not_set_the_flash
end
end

context "destroying" do
setup do
@document = Document.new
Document.stubs(:find).returns(@document)

@document.expects(:destroy).once
delete :destroy, :id => 1
end

should_assign_to(:document){@document}
should_respond_with :success
should_render_without_layout
should_not_set_the_flash
end
end
end

class DocumentsControllerTest < ActionController::TestCase
# Replace this with your real tests.
def test_truth
assert true
should_allow_everything_for('admin', :admin)
should_allow_everything_for('a member', :quentin)

context "as a visitor" do
should_require_login_for :new, :create, :destroy
end
end
39 changes: 30 additions & 9 deletions test/shoulda_macros/bounce_macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,36 @@ def should_require_login_for(*tests)
end
end

def should_not_authorize(test)
model = test_unit_class.class.name.gsub(/ControllerTest$/, '').singularize.constantize
def should_not_authorize
should_not_authorize_for :all
end

setup do
eval test
end
def should_not_authorize_for(*tests)
model = test_unit_class.name.gsub(/ControllerTest$/, '').singularize.constantize

tests = [:index, :show, :new, :create, :edit, :update, :destroy] if tests.include?(:all)

should_not_assign_to model.name.to_sym
should_respond_with 401
should_render_without_layout
should_not_set_the_flash
tests.each do |test|
action = case test
when :index then "get :index"
when :show then "get :show"
when :new then "get :new"
when :create then "post :create"
when :edit then "get :edit"
when :update then "put :update"
when :destroy then "delete :destroy"
else test
end

context "attempting to #{action}" do
setup do
eval action
end

should_not_assign_to model.name.to_sym
should_respond_with 401
should_render_without_layout
should_not_set_the_flash
end
end
end

0 comments on commit 8db751d

Please sign in to comment.