-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor our test files and remove mocha
- Loading branch information
Showing
7 changed files
with
48 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,21 @@ | ||
$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) | ||
|
||
require "rspec" | ||
require "mocha/api" | ||
require "her" | ||
|
||
# Require everything in `spec/support` | ||
Dir[File.expand_path('../../spec/support/**/*.rb', __FILE__)].map(&method(:require)) | ||
|
||
RSpec.configure do |config| | ||
config.mock_with :mocha | ||
config.include Her::Testing::Macros::ModelMacros | ||
|
||
config.before :each do | ||
@globals = [] | ||
@spawned_models = [] | ||
end | ||
|
||
config.after :each do | ||
@globals.each do |global| | ||
Object.instance_eval { remove_const global } if Object.const_defined?(global) | ||
end | ||
end | ||
end | ||
|
||
class Hash | ||
def to_json; MultiJson.dump(self); end | ||
end | ||
|
||
class Array | ||
def to_json; MultiJson.dump(self); end | ||
end | ||
|
||
def spawn_model(klass, &block) | ||
if klass =~ /::/ | ||
base, submodel = klass.split(/::/).map{ |s| s.to_sym } | ||
Object.const_set(base, Module.new) unless Object.const_defined?(base) | ||
Object.const_get(base).module_eval do | ||
remove_const submodel if constants.include?(submodel) | ||
submodel = const_set(submodel, Class.new) | ||
submodel.send(:include, Her::Model) | ||
submodel.class_eval(&block) if block_given? | ||
@spawned_models.each do |model| | ||
Object.instance_eval { remove_const model } if Object.const_defined?(model) | ||
end | ||
@globals << base | ||
else | ||
Object.instance_eval { remove_const klass } if Object.const_defined?(klass) | ||
Object.const_set(klass, Class.new).send(:include, Her::Model) | ||
Object.const_get(klass).class_eval(&block) if block_given? | ||
@globals << klass.to_sym | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Array | ||
def to_json | ||
MultiJson.dump(self) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Hash | ||
def to_json | ||
MultiJson.dump(self) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Her | ||
module Testing | ||
module Macros | ||
module ModelMacros | ||
# Create a class and automatically inject Her::Model into it | ||
def spawn_model(klass, &block) | ||
if klass =~ /::/ | ||
base, submodel = klass.split(/::/).map{ |s| s.to_sym } | ||
Object.const_set(base, Module.new) unless Object.const_defined?(base) | ||
Object.const_get(base).module_eval do | ||
remove_const submodel if constants.include?(submodel) | ||
submodel = const_set(submodel, Class.new) | ||
submodel.send(:include, Her::Model) | ||
submodel.class_eval(&block) if block_given? | ||
end | ||
|
||
@spawned_models << base | ||
else | ||
Object.instance_eval { remove_const klass } if Object.const_defined?(klass) | ||
Object.const_set(klass, Class.new).send(:include, Her::Model) | ||
Object.const_get(klass).class_eval(&block) if block_given? | ||
|
||
@spawned_models << klass.to_sym | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |