Permalink
Browse files

Add a define_model a bit like remarkable to make it work on Ruby 1.9 too

  • Loading branch information...
1 parent 7e20f8c commit b3933d5a8711fb8183768cd69fd87a0e6b8f1e8e Joseph HALTER committed Aug 13, 2009
@@ -3,12 +3,7 @@
describe "have_column_matcher" do
before :all do
- class Item < Sequel::Model
- end
- end
-
- after :all do
- Object.send(:remove_const, :Item)
+ define_model :Item
end
subject{ Item }
@@ -23,7 +18,7 @@ class Item < Sequel::Model
@matcher = have_column :name
@matcher.matches? subject
@matcher.failure_message.should == "expected Item to have a column :name"
- @matcher.negative_failure_message.should == "expected Item to not have a column :name"
+ @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
end
end
describe "with type as symbol" do
@@ -35,7 +30,7 @@ class Item < Sequel::Model
@matcher = have_column :password, :type => :string
@matcher.matches? subject
@matcher.failure_message.should == "expected Item to have a column :password with type string"
- @matcher.negative_failure_message.should == "expected Item to not have a column :password with type string"
+ @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
end
end
describe "with type as object" do
@@ -47,37 +42,37 @@ class Item < Sequel::Model
@matcher = have_column :password, :type => String
@matcher.matches? subject
@matcher.failure_message.should == "expected Item to have a column :password with type String"
- @matcher.negative_failure_message.should == "expected Item to not have a column :password with type String"
+ @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
end
it "should explicit found type if different than expected" do
@matcher = have_column :name, :type => Integer
@matcher.matches? subject
@matcher.failure_message.should == "expected Item to have a column :name with type Integer (type found: string, varchar(255))"
- @matcher.negative_failure_message.should == "expected Item to not have a column :name with type Integer (type found: string, varchar(255))"
+ @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
end
end
describe "on anonymous Sequel::Model class" do
it "should set failure messages" do
@matcher = have_column :password
- @matcher.matches? Sequel::Model(:items)
- @matcher.failure_message.should == "expected items to have a column :password"
- @matcher.negative_failure_message.should == "expected items to not have a column :password"
+ @matcher.matches? Sequel::Model(:comments)
+ @matcher.failure_message.should == "expected comments to have a column :password"
+ @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
end
end
describe "on Sequel::Model class" do
it "should set failure messages" do
@matcher = have_column :password
@matcher.matches? Item
@matcher.failure_message.should == "expected Item to have a column :password"
- @matcher.negative_failure_message.should == "expected Item to not have a column :password"
+ @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
end
end
describe "on Sequel::Model instance" do
it "should set failure messages" do
@matcher = have_column :password
@matcher.matches? Item.new
@matcher.failure_message.should == "expected #<Item @values={}> to have a column :password"
- @matcher.negative_failure_message.should == "expected #<Item @values={}> to not have a column :password"
+ @matcher.negative_failure_message.should == @matcher.failure_message.gsub("to have", "to not have")
end
end
end
@@ -3,16 +3,10 @@
describe "have_many_to_many_matcher" do
before :all do
- class Comment < Sequel::Model
+ define_model(:Item)
+ define_model(:Comment){
many_to_many :items
- end
- class Item < Sequel::Model
- end
- end
-
- after :all do
- Object.send(:remove_const, :Comment)
- Object.send(:remove_const, :Item)
+ }
end
subject{ Comment }
@@ -3,16 +3,10 @@
describe "have_many_to_one_matcher" do
before :all do
- class Comment < Sequel::Model
+ define_model(:Item)
+ define_model(:Comment){
many_to_one :item
- end
- class Item < Sequel::Model
- end
- end
-
- after :all do
- Object.send(:remove_const, :Comment)
- Object.send(:remove_const, :Item)
+ }
end
subject{ Comment }
@@ -3,16 +3,10 @@
describe "have_one_to_many_matcher" do
before :all do
- class Comment < Sequel::Model
- end
- class Item < Sequel::Model
+ define_model(:Item){
one_to_many :comments
- end
- end
-
- after :all do
- Object.send(:remove_const, :Comment)
- Object.send(:remove_const, :Item)
+ }
+ define_model(:Comment)
end
subject{ Item }
View
@@ -1,6 +1,7 @@
require "spec"
require "rubygems"
require "sequel"
+require "sequel/extensions/inflector"
require "sequel/extensions/migration"
require File.join(File.dirname(__FILE__), "..", "lib", "rspec_sequel_matchers")
@@ -11,6 +12,20 @@
puts "sqlite not available. Install it with: sudo gem install sqlite3-ruby"
end
+def define_model(model, &block)
+ @defined_models ||= []
+ @defined_models << model
+ klass = Object.const_set model, Sequel::Model(model.to_s.tableize.to_sym)
+ klass.class_eval &block if block_given?
+end
+
+def undefine_models
+ @defined_models.each{|model|
+ Object.send(:remove_const, model)
+ }
+ @defined_models = []
+end
+
Spec::Runner.configure do |config|
config.include(RspecSequel::Matchers)
@@ -31,4 +46,9 @@
end
end
+ # undefine models defined via define_model
+ config.after(:all) do
+ undefine_models
+ end
+
end
@@ -3,16 +3,12 @@
describe "validate_exact_length_matcher" do
before :all do
- class Item < Sequel::Model
+ define_model(:Item){
plugin :validation_helpers
def validate
validates_exact_length 4, :name, :allow_nil => true
end
- end
- end
-
- after :all do
- Object.send(:remove_const, :Item)
+ }
end
subject{ Item }
@@ -3,16 +3,12 @@
describe "validate_presence_matcher" do
before :all do
- class Item < Sequel::Model
+ define_model(:Item){
plugin :validation_helpers
def validate
validates_presence [:id, :name], :allow_nil => true
end
- end
- end
-
- after :all do
- Object.send(:remove_const, :Item)
+ }
end
subject{ Item }

0 comments on commit b3933d5

Please sign in to comment.