Created
March 13, 2012 00:41
-
-
Save rickharris/2025775 to your computer and use it in GitHub Desktop.
admin_login for integration tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class Admin::AdministerFaqsTest < ActionDispatch::IntegrationTest | |
an "Admin creating a new FAQ section" do | |
before do | |
admin_login | |
end | |
it "is able to view available plans" do | |
visit "/admin/faq_sections/new" | |
fill_in "Name", :with => "New FAQ Section" | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'simplecov' | |
SimpleCov.start if ENV['COVERAGE'] | |
ENV["RAILS_ENV"] = "test" | |
require File.expand_path(File.dirname(__FILE__) + "/../config/environment") | |
require 'rails/test_help' | |
require 'authlogic/test_case' | |
require 'mocha' | |
require 'nutrasuite' | |
require 'database_cleaner' | |
require 'capybara/rails' | |
# Bump up the log level for performance | |
Rails.logger.level = 4 | |
# require all of our mock scripts | |
Dir[File.join(File.dirname(__FILE__), "mocks/**/*.rb")].each do |f| | |
require f | |
end | |
Sunspot.session = Sunspot::Rails::StubSessionProxy.new(Sunspot.session) unless @use_sunspot | |
class ActiveSupport::TestCase | |
self.use_transactional_fixtures = true | |
def self.use_sunspot | |
setup do | |
Sunspot.session = Sunspot.session.original_session unless @use_sunspot | |
end | |
teardown do | |
Sunspot.session = Sunspot::Rails::StubSessionProxy.new(Sunspot.session) | |
end | |
end | |
# Asserts that the field specified isn't valid | |
def assert_not_valid(model, field) | |
assert_equal false, model.valid? | |
assert_equal true, model.errors[field].any? | |
end | |
end | |
class ActionController::TestCase | |
setup :activate_authlogic | |
def build_user_and_login | |
user = Factory.create(:subscription).user | |
user.credit_card = Factory.create(:credit_card, :user => user) | |
RandomMessage.create!(:content => "This is a random message!", :icon => "alien") | |
login(user) | |
user | |
end | |
def login(user) | |
PersonSession.create(user) | |
end | |
def admin_login(admin = nil) | |
admin = Factory.create(:administrator) if !admin | |
AdministratorSession.create(admin) | |
admin | |
end | |
end | |
# Transactional fixtures do not work with Selenium tests, because Capybara | |
# uses a separate server thread, which the transactions would be hidden | |
# from. We hence use DatabaseCleaner to truncate our test database. | |
DatabaseCleaner.strategy = :truncation | |
class ActionDispatch::IntegrationTest | |
# Make the Capybara DSL available in all integration tests | |
include Capybara::DSL | |
# Stop ActiveRecord from wrapping tests in transactions | |
self.use_transactional_fixtures = false | |
setup :activate_authlogic | |
def build_user_and_login | |
user = Factory.create(:active_user) | |
user.credit_card = Factory.create(:credit_card, :user => user) | |
RandomMessage.create!(:content => "This is a random message!", :icon => "alien") | |
post "person_session", { :person_session => { :email => user.email, :password => user.password } } | |
user | |
end | |
def admin_login(admin = nil) | |
admin = Factory.create(:administrator) if !admin | |
AdministratorSession.create(admin) | |
admin | |
end | |
teardown do | |
DatabaseCleaner.clean # Truncate the database | |
Capybara.reset_sessions! # Forget the (simulated) browser state | |
Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment