Skip to content

Instantly share code, notes, and snippets.

Created November 19, 2014 00:54
Show Gist options
  • Save anonymous/0fd8fd2c9e6a7416321e to your computer and use it in GitHub Desktop.
Save anonymous/0fd8fd2c9e6a7416321e to your computer and use it in GitHub Desktop.
guard :rspec, cmd: 'bundle exec rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
#watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/acceptance" }
watch('spec/rails_helper.rb') { "spec" }
# Capybara features specs
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
end
#spec/spec_helper.rb
require 'spork'
require 'capybara/rspec'
Spork.prefork do
require "rails/application"
Spork.trap_method(Rails::Application, :reload_routes!)
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rubygems'
require 'faker'
require 'database_cleaner'
require 'shoulda-matchers'
end
Spork.each_run do
load "#{Rails.root}/config/routes.rb"
Dir["#{Rails.root}/lib/**/*.rb"].each {|f| load f}
#use thin for capybara
Capybara.server do |app, port|
require 'rack/handler/thin'
Rack::Handler::Thin.run(app, :Port => port)
end
Capybara.server_port = 3001
Capybara.default_host = 'http://localhost:3001'
OmniAuth.config.test_mode = true
# This code will be run each time you run your specs.
RSpec.configure do |config|
config.mock_with :rspec
config.use_transactional_fixtures = false
end
FactoryGirl.reload
end
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/modules/**/*.rb")].each {|f| require f}
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
Dir[Rails.root.join("spec/matchers/**/*.rb")].each {|f| require f}
Dir[Rails.root.join("spec/core_ext/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
config.include LoginHelpers, type: :feature
config.include DownloadHelpers, type: :feature
config.include InputExampleGroup, type: :input
Capybara.register_driver :firefox do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = DownloadHelpers::PATH.to_s #set download dir
profile['browser.download.folderList'] = 2 #prevent open with dialog
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/plain"
Capybara::Selenium::Driver.new(app, browser: :firefox, profile: profile)
end
Capybara.javascript_driver = :firefox
Faker::Config.locale = 'en'
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
PrimaryAtDeviceArea.seed
PrimaryUseArea.seed
ItemType.seed
end
config.after(:each) do
DatabaseCleaner.clean
end
config.include FactoryGirl::Syntax::Methods
end
#spec/support/shared_examples_for_downloadable.rb
shared_examples "a downloadable" do
def csv_template(collection, fk_attr = nil)
#generate csvs to test against.
#if fk true, attach created_at to end of csv row
CSV.generate(col_sep: ",") do |csv|
if fk_attr
csv << model.column_names + ['created_at']
else
csv << model.column_names
end
collection.each do |ele|
if fk_attr
csv << ele.attributes.values_at(*model.column_names) + [ele.created_at]
else
csv << ele.attributes.values_at(*model.column_names)
end
end
end
end
include_context "subject class"
let(:collection) { [create(factory), create(factory)] }
it "responds .to_csv" do
expect(model).to respond_to(:to_csv)
end
it ".to_csv(collection)" do
#header = model.column_names.to_sentence(last_word_connector: ",").gsub(/\s+/, "") + "\n"
expect(model.to_csv(collection)).to eq(csv_template(collection))
end
it ".to_csv(collection, fk_attr)" do
expect(model.to_csv(collection, {model.column_names.last => ['created_at']}) ).to eq(csv_template(collection, true))
end
end
shared_examples "its downloadable", js: true do
before(:each) do
login
DownloadHelpers.clear_downloads
end
after(:each) do
DownloadHelpers.clear_downloads
end
def path
eval(subject.class.name.downcase.underscore.pluralize + '_path')
end
scenario 'should have download button' do
visit path
expect(page).to have_link("Download", href: path + '.csv')
end
scenario 'should download a csv' do
visit path + '.csv'
expect(DownloadHelpers.download_path).to include("#{subject.class.name.downcase.underscore.pluralize}-downloadable.csv")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment