Skip to content

Instantly share code, notes, and snippets.

View caseydreier's full-sized avatar

Casey Dreier caseydreier

View GitHub Profile
class Contact < NoTable
# Define our "fields"
column :id, :integer
column :name, :string
column :email, :string
column :message, :string
validates_presence_of :email, :name, :message
class ProjectsControllerTest < ActionController::TestCase
def setup
set_test_host('secondary-domain.com')
end
test "routes do not exist for secondary-domain.com" do
assert_not_routing('/projects' , { :controller => "projects", :action => "index" })
assert_not_routing('/projects/1' , { :controller => "projects", :action => "show", :id => "1" })
end
# Helper Method for setting the requesting host in a Rails functional test
# Mocks the proper response for the domain() method, as well as the
# @request.host value.
def set_test_host(host)
@request.host = host
domain = host.split('.').last(2).join('.')
ActionController::TestRequest.any_instance.stubs(:domain).returns(domain)
end
# Assert that a specified route does not exist inside a functional test
def assert_not_routing(path, options, defaults={}, extras={}, message=nil)
assert_raise ActionController::RoutingError do
assert_routing(path, options, defaults, extras, message)
end
end
sudo gem update --system
# Load Webrat 0.7.0
require 'webrat'
# I'm going to be contrary and use Test/Unit instead of RSpec
require 'test/unit/assertions'
# Add Test/Unit assertions to Cucumber
World(Test::Unit::Assertions)
# Here is where we configure Webrat to use Selenium.
@caseydreier
caseydreier / factory_collections_helper.rb
Created January 26, 2010 21:23
Handy way to create collection of saved or unsaved model objects with Factory Girl.
class ActiveSupport::TestCase
# Helper method_missing override to create nicely-named collections of factory objects
# For example: create_factory_collection_for_current_students(15,true)
# will call create_factory_collection(:current_students, 15, true)
def method_missing(requested_method, *args)
if requested_method.to_s =~ /\Acreate_factory_collection_for_(\w+)\Z/
__send__("create_factory_collection", $1, *args)
else
# Otherwise handle normally #
# Stubs saving or creating methods in the database for Models associated with Database views.
# In these cases, we specify the ID in the factory so we can still use them for associations.
# If we didn't stub these actions out, the Factory would try to create them in the (read-only) view
# when creating associations.
database_view_models = ['Current::Course','Current::CurrentStudent']
methods_to_override = [:save, :save!, :create, :create!]
database_view_models.each do |klass|
methods_to_override.each do |method|