Skip to content

Instantly share code, notes, and snippets.

@mallain
Created July 22, 2010 15:34
Show Gist options
  • Save mallain/486121 to your computer and use it in GitHub Desktop.
Save mallain/486121 to your computer and use it in GitHub Desktop.
require 'test_helper'
class Hr::AlertCollaboratorsControllerTest < ActionController::TestCase
available_testing_locales.each do |lang|
context "(locale=#{lang.to_s.upcase}) logged in" do
setup do
generate_setup(:lang => lang, :instanciate => ['alert_collaborator'])
end
should_get_action :index
should_get_action :new
context "on POST to :create (valid data)" do
setup do
AlertCollaborator.any_instance.expects(:save).returns(true).once
AlertCollaborator.any_instance.stubs(:id).returns(1001)
post :create, :alert_collaborator => {}
end
should_assign_to :alert_collaborator, :class => AlertCollaborator
should_respond_with :redirect
should_redirect_to("alert_collaborator index page"){hr_alert_collaborators_path}
end
context "on POST to :create (invalid data)" do
setup do
AlertCollaborator.any_instance.expects(:save).returns(false).once
post :create, :alert_collaborator => {}
end
should_assign_to :alert_collaborator, :class => AlertCollaborator
should_respond_with :success
should_render_with_layout
should_render_template :new
should_not_set_the_flash
end
context "on GET to :show" do
setup do
get :show, {:id => @alert_collaborator.id}
end
should_respond_with :success
should_render_with_layout
should_render_template :show
should_not_set_the_flash
end
context "on GET to :edit" do
setup do
get :edit, :id => @alert_collaborator.id
end
should_respond_with :success
should_render_with_layout
should_render_template :edit
should_not_set_the_flash
end
context "on PUT to :update (valid data)" do
setup do
@alert_collaborator.expects(:update_attributes).returns(true).once
put :update, :id => @alert_collaborator.id, :alert_collaborator => {}
end
should_assign_to :alert_collaborator, :class => AlertCollaborator
should_respond_with :redirect
should_redirect_to("alert_collaborator index page"){hr_alert_collaborators_path}
end
context "on PUT to :update (invalid data)" do
setup do
@alert_collaborator.expects(:update_attributes).returns(false).once
put :update, :id => @alert_collaborator.id, :alert_collaborator => {}
end
should_assign_to :alert_collaborator, :class => AlertCollaborator
should_respond_with :success
should_render_template :edit
end
context "on DELETE to :destroy" do
setup do
@alert_collaborator.expects(:destroy).returns(true).once
delete :destroy, :id => @alert_collaborator.id
end
should_assign_to(:alert_collaborator){@alert_collaborator}
should_respond_with :redirect
should_redirect_to("alert_collaborator index page"){hr_alert_collaborators_path}
end
end
end
end
ruby functional/hr/alert_collaborators_controller_test.rb
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF (Every tests Fail)
62) Failure:
test: (locale=FR) logged in on PUT to :update (invalid data) should render template :edit. (Hr::AlertCollaboratorsControllerTest)
[shoulda (2.10.3) lib/shoulda/action_controller/macros.rb:164:in `__bind_1279812770_248932'
shoulda (2.10.3) lib/shoulda/context.rb:362:in `call'
shoulda (2.10.3) lib/shoulda/context.rb:362:in `test: (locale=FR) logged in on PUT to :update (invalid data) should render template :edit. ']:
expecting <"edit"> but rendering with <"">
63) Failure:
test: (locale=FR) logged in on PUT to :update (invalid data) should respond with success. (Hr::AlertCollaboratorsControllerTest)
[shoulda (2.10.3) lib/shoulda/assertions.rb:55:in `assert_accepts'
shoulda (2.10.3) lib/shoulda/action_controller/macros.rb:124:in `__bind_1279812770_353465'
shoulda (2.10.3) lib/shoulda/context.rb:362:in `call'
shoulda (2.10.3) lib/shoulda/context.rb:362:in `test: (locale=FR) logged in on PUT to :update (invalid data) should respond with success. ']:
Expected response to be a 200, but was 302
64) Failure:
test: (locale=FR) logged in on PUT to :update (valid data) should assign @alert_collaborator with a kind of AlertCollaborator. (Hr::AlertCollaboratorsControllerTest)
[shoulda (2.10.3) lib/shoulda/assertions.rb:55:in `assert_accepts'
shoulda (2.10.3) lib/shoulda/action_controller/macros.rb:97:in `__bind_1279812770_462603'
shoulda (2.10.3) lib/shoulda/context.rb:362:in `call'
shoulda (2.10.3) lib/shoulda/context.rb:362:in `test: (locale=FR) logged in on PUT to :update (valid data) should assign @alert_collaborator with a kind of AlertCollaborator. ']:
Expected action to assign a value for @alert_collaborator
65) Failure:
test: (locale=FR) logged in on PUT to :update (valid data) should redirect to alert_collaborator index page. (Hr::AlertCollaboratorsControllerTest)
[shoulda (2.10.3) lib/shoulda/action_controller/macros.rb:202:in `__bind_1279812770_688327'
shoulda (2.10.3) lib/shoulda/context.rb:362:in `call'
shoulda (2.10.3) lib/shoulda/context.rb:362:in `test: (locale=FR) logged in on PUT to :update (valid data) should redirect to alert_collaborator index page. ']:
Expected response to be a redirect to <http://test.host/hr/alert_collaborators> but was a redirect to <http://test.host/login>.
66) Failure:
test: (locale=FR) logged in on PUT to :update (valid data) should respond with redirect. (Hr::AlertCollaboratorsControllerTest)
[/test_helper.rb:119:in `generate_setup'
functional/hr/alert_collaborators_controller_test.rb:7:in `__bind_1279812770_750706'
shoulda (2.10.3) lib/shoulda/context.rb:380:in `call'
shoulda (2.10.3) lib/shoulda/context.rb:380:in `run_current_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:379:in `each'
shoulda (2.10.3) lib/shoulda/context.rb:379:in `run_current_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:371:in `run_all_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:375:in `run_parent_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:359:in `test: (locale=FR) logged in on PUT to :update (valid data) should respond with redirect. ']:
not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked: #<AnyInstance:UserSession>.save(any_parameters)
- expected exactly once, not yet invoked: #<AlertCollaborator:0x7f7c9bae8d68>.update_attributes(any_parameters)
satisfied expectations:
- allowed any number of times, already invoked once: Feedback(id: integer, feedback_time: datetime).find(:all, anything)
- allowed any number of times, already invoked twice: Feedback(id: integer, feedback_time: datetime).find(any_parameters)
- allowed any number of times, already invoked 4 times: #<AnyInstance:UserSession>.id(any_parameters)
- allowed any number of times, not yet invoked: AlertCollaborator(id: integer, feedback_id: integer, agency_id: integer, collaborator_id: integer, created_at: datetime, updated_at: datetime).find(:all, anything)
- allowed any number of times, not yet invoked: AlertCollaborator(id: integer, feedback_id: integer, agency_id: integer, collaborator_id: integer, created_at: datetime, updated_at: datetime).find(any_parameters)
66 tests, 78 assertions, 66 failures, 0 errors
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'
require 'factory_girl'
require 'shoulda'
require 'faker'
require "authlogic/test_case"
require 'i18n'
class ActiveSupport::TestCase
# Transactional fixtures accelerate your tests by wrapping each test method
# in a transaction that's rolled back on completion. This ensures that the
# test database remains unchanged so your fixtures don't have to be reloaded
# between every test method. Fewer database queries means faster tests.
#
# Read Mike Clark's excellent walkthrough at
# http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
#
# Every Active Record database supports transactions except MyISAM tables
# in MySQL. Turn off transactional fixtures in this case; however, if you
# don't care one way or the other, switching from MyISAM to InnoDB tables
# is recommended.
#
# The only drawback to using transactional fixtures is when you actually
# need to test transactions. Since your test is bracketed by a transaction,
# any transactions started in your code will be automatically rolled back.
self.use_transactional_fixtures = true
# Instantiated fixtures are slow, but give you @david where otherwise you
# would need people(:david). If you don't want to migrate your existing
# test cases which use the @david style and don't mind the speed hit (each
# instantiated fixtures translates to a database query per test method),
# then set this back to true.
self.use_instantiated_fixtures = false
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
#fixtures :all
end
# Redefine #t method of TranslationHelper
# With this hack, all missing translation will raise a exception
module ActionView
module Helpers
module TranslationHelper
def t(key, options = {})
options[:raise] = true
I18n.translate(scope_key_by_partial(key), options)
end
end
end
end
# Define active_authlogic into all setup
class ActionController::TestCase
setup :activate_authlogic
end
# Generate an action
# param action : define the "get" action (string)
# ex : should_get_action("index")
def should_get_action(action)
context "on GET to #{action}" do
setup do
get action
end
should_respond_with :success
should_render_with_layout
should_render_template action
should_not_set_the_flash
end
end
# Return available locales for testing into ControllerTest
def available_testing_locales
I18n.available_locales
end
# Instanciate stub objects
# param name : Describe the name of model (string)
# ex : instanciate_stub_object('feedback')
def instanciate_stub_object(name)
# Instanciate objects array
objects = []
# Instanciate first object
instance_variable_set("@#{name}".to_sym, Factory.stub(name.to_sym))
# Instanciate five objects
(1..5).each do |i|
instance_variable_set("@#{name}_#{i}".to_sym, Factory.stub(name.to_sym))
instance_variable_get("@#{name}_#{i}").id = rand(3000)
objects << instance_variable_get("@#{name}_#{i}")
end
# Find method return an instanciate object
name.classify.constantize.stubs(:find).returns(instance_variable_get("@#{name}"))
# Find method with "all" parameter will return an array of instanciate objects
name.classify.constantize.stubs(:find).with(:all, anything).returns(objects)
end
# Generate setup with parameter
# param lang : Define the default_locale (string)
# param instanciate : Define the list of objects we have to instanciate in this setup Array with Hash
# ex : generate_setup(:lang => 'fr')
# ex : generate_setup(:lang => 'en', :instanciate => ['feedback', 'claim_collection')
def generate_setup(params={})
# Create at least one agency
Factory(:agency)
# Feedback is need everywhere
instanciate_stub_object('feedback')
UserSession.any_instance.expects(:save).returns(true).once
UserSession.any_instance.stubs(:id).returns(1001)
# Instanciate every object included in params[:instanciate] variable
params[:instanciate].each do |object|
instanciate_stub_object(object)
end
# Define setup locale
I18n.default_locale = params[:lang]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment