Module: Jax::Testing::RailsEnvironment
- Defined in:
- lib/jax/testing/rails_environment.rb
Overview
Set up a subclass of Rails::Application and set ‘config.root` to a temporary path prior to calling #setup_rails_environment.
Example:
include Jax::Testing::RailsEnvironment
setup do
setup_rails_environment
create_file "app/assets/jax/resources/people/default.resource" do |f|
f.puts "name: Colin"
end
create_file "version", "1.0.0"
end
Instance Method Summary collapse
- #append_to_file(file, content = nil, &block) ⇒ Object
- #asset(path) ⇒ Object
- #assets ⇒ Object
- #create_asset(file, content = nil, &block) ⇒ Object
- #create_directory(dir) ⇒ Object
- #create_empty_directory(dir) ⇒ Object
- #create_file(file, content = nil) ⇒ Object
- #create_shader(path, contents = {}) ⇒ Object
- #local(path) ⇒ Object
- #route(content) ⇒ Object
-
#setup_rails_environment ⇒ Object
WARNING: Set Rails.application.config.root to some temporary directory name before calling this method! This method deletes the Rails root in order to set up the fixture for the next test run.
Instance Method Details
#append_to_file(file, content = nil, &block) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/jax/testing/rails_environment.rb', line 76 def append_to_file(file, content = nil, &block) if File.file?(local file) File.open(local(file), "w+") do |f| f.puts content if content yield f if block_given? end else create_file(file, content, &block) end end |
#asset(path) ⇒ Object
70 71 72 73 74 |
# File 'lib/jax/testing/rails_environment.rb', line 70 def asset(path) asset = assets.find_asset(path) raise "asset not found: #{path} in #{@asset_dir}" unless asset asset.to_s end |
#assets ⇒ Object
66 67 68 |
# File 'lib/jax/testing/rails_environment.rb', line 66 def assets ::Rails.application.assets end |
#create_asset(file, content = nil, &block) ⇒ Object
56 57 58 |
# File 'lib/jax/testing/rails_environment.rb', line 56 def create_asset(file, content = nil, &block) create_file File.join("app/assets/jax", file), content, &block end |
#create_directory(dir) ⇒ Object
44 45 46 |
# File 'lib/jax/testing/rails_environment.rb', line 44 def create_directory(dir) FileUtils.mkdir_p local(dir) end |
#create_empty_directory(dir) ⇒ Object
39 40 41 42 |
# File 'lib/jax/testing/rails_environment.rb', line 39 def create_empty_directory dir FileUtils.rm_rf local(dir) create_directory dir end |
#create_file(file, content = nil) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/jax/testing/rails_environment.rb', line 48 def create_file(file, content = nil) create_directory File.dirname(file) File.open(local(file), "w") do |f| f.puts content if content yield f if block_given? end end |
#create_shader(path, contents = {}) ⇒ Object
60 61 62 63 64 |
# File 'lib/jax/testing/rails_environment.rb', line 60 def create_shader(path, contents = {}) create_asset File.join("shaders", path, "common.glsl"), contents[:common] if contents[:common] create_asset File.join("shaders", path, "fragment.glsl"), contents[:fragment] if contents[:fragment] create_asset File.join("shaders", path, "vertex.glsl"), contents[:vertex] if contents[:vertex] end |
#local(path) ⇒ Object
35 36 37 |
# File 'lib/jax/testing/rails_environment.rb', line 35 def local(path) path =~ /^#{Regexp::escape ::Rails.root.to_s}/ ? path : ::Rails.root.join(path).to_s end |
#route(content) ⇒ Object
87 88 89 90 91 |
# File 'lib/jax/testing/rails_environment.rb', line 87 def route(content) append_to_file "config/routes.rb" # create it if missing routes = File.read(local "config/routes.rb") create_file("config/routes.rb", routes.sub(/Rails.application.routes.draw do$/, "\\1#{content}")) end |
#setup_rails_environment ⇒ Object
WARNING: Set Rails.application.config.root to some temporary directory name before calling this method! This method deletes the Rails root in order to set up the fixture for the next test run.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/jax/testing/rails_environment.rb', line 23 def setup_rails_environment root = ::Rails.root.to_s create_empty_directory root create_directory 'app/assets' # required for Rails to pick up assets paths create_file "config/routes.rb" do |f| f.puts "Rails.application.routes.draw do\nend" end Jax.reset_config! end |