Skip to content

Commit

Permalink
Refactor our test files and remove mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
remi committed Apr 6, 2013
1 parent 07f0b84 commit 058a7b3
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 35 deletions.
1 change: 0 additions & 1 deletion her.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Gem::Specification.new do |s|

s.add_development_dependency "rake", "~> 10.0"
s.add_development_dependency "rspec", "~> 2.13"
s.add_development_dependency "mocha", "~> 0.13"

s.add_runtime_dependency "activemodel", ">= 3.0.0"
s.add_runtime_dependency "activesupport", ">= 3.0.0"
Expand Down
2 changes: 1 addition & 1 deletion spec/model/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@

it "delegates eql? to ==" do
other = Object.new
user.expects(:==).with(other).returns(true)
user.should_receive(:==).with(other).and_return(true)
user.eql?(other).should be_true
end

Expand Down
2 changes: 1 addition & 1 deletion spec/model/orm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def to_params

it "delegates eql? to ==" do
other = Object.new
user.expects(:==).with(other).returns(true)
user.should_receive(:==).with(other).and_return(true)
user.eql?(other).should be_true
end

Expand Down
39 changes: 7 additions & 32 deletions spec/spec_helper.rb
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
5 changes: 5 additions & 0 deletions spec/support/extensions/array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Array
def to_json
MultiJson.dump(self)
end
end
5 changes: 5 additions & 0 deletions spec/support/extensions/hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Hash
def to_json
MultiJson.dump(self)
end
end
29 changes: 29 additions & 0 deletions spec/support/macros/model_macros.rb
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

0 comments on commit 058a7b3

Please sign in to comment.