spec/readme_spec.rb in with_model-0.3.2 vs spec/readme_spec.rb in with_model-1.0.0

- old
+ new

@@ -1,86 +1,105 @@ - require 'spec_helper' +require 'spec_helper' - describe "A blog post" do +describe "A blog post" do + before :all do + module SomeModule; end + end - with_model :BlogPost do - # The table block works just like a migration. - table do |t| - t.string :title - t.timestamps - end + after :all do + Object.send :remove_const, :SomeModule + end - # The model block works just like the class definition. - model do - include SomeModule - has_many :comments - validates_presence_of :title + with_model :BlogPost do + # The table block works just like a migration. + table do |t| + t.string :title + t.timestamps + end - def self.some_class_method - 'chunky' - end + # The model block works just like the class definition. + model do + include SomeModule + has_many :comments + validates_presence_of :title - def some_instance_method - 'bacon' - end + def self.some_class_method + 'chunky' end - end - # with_model classes can have associations. - with_model :Comment do - table do |t| - t.string :text - t.belongs_to :blog_post - t.timestamps + def some_instance_method + 'bacon' end - - model do - belongs_to :blog_post - end end + end - it "can be accessed as a constant" do - BlogPost.should be + # with_model classes can have associations. + with_model :Comment do + table do |t| + t.string :text + t.belongs_to :blog_post + t.timestamps end - it "has the module" do - BlogPost.include?(SomeModule).should be_true + model do + belongs_to :blog_post end + end - it "has the class method" do - BlogPost.some_class_method.should == 'chunky' - end + it "can be accessed as a constant" do + expect(BlogPost).to be + end - it "has the instance method" do - BlogPost.new.some_instance_method.should == 'bacon' - end + it "has the module" do + expect(BlogPost.include?(SomeModule)).to be_true + end - it "can do all the things a regular model can" do - record = BlogPost.new - record.should_not be_valid - record.title = "foo" - record.should be_valid - record.save.should be_true - record.reload.should == record - record.comments.create!(:text => "Lorem ipsum") - record.comments.count.should == 1 - end + it "has the class method" do + expect(BlogPost.some_class_method).to eq 'chunky' end - describe "another example group" do - it "should not have the constant anymore" do - defined?(BlogPost).should be_false - end + it "has the instance method" do + expect(BlogPost.new.some_instance_method).to eq 'bacon' end - describe "with table options" do - with_model :WithOptions do - table :id => false do |t| - t.string 'foo' - t.timestamps - end - end + it "can do all the things a regular model can" do + record = BlogPost.new + expect(record).to_not be_valid + record.title = "foo" + expect(record).to be_valid + expect(record.save).to be_true + expect(record.reload).to eq record + record.comments.create!(:text => "Lorem ipsum") + expect(record.comments.count).to eq 1 + end - it "should respect the additional options" do - WithOptions.columns.map(&:name).should_not include("id") + # with_model classes can have inheritance. + class Car < ActiveRecord::Base + self.abstract_class = true + end + + with_model :Ford, superclass: Car do + end + + it "has a specified superclass" do + expect(Ford < Car).to be_true + end +end + +describe "another example group" do + it "does not have the constant anymore" do + expect(defined?(BlogPost)).to be_false + end +end + +describe "with table options" do + with_model :WithOptions do + table :id => false do |t| + t.string 'foo' + t.timestamps end end + + it "respects the additional options" do + expect(WithOptions.columns.map(&:name)).to_not include("id") + end +end