spec/trackoid_spec.rb in trackoid-0.1.0 vs spec/trackoid_spec.rb in trackoid-0.1.1

- old
+ new

@@ -7,24 +7,34 @@ field :name # Dummy field track :visits end describe Mongoid::Tracking do + + before(:all) do + @trackoid_version = File.read(File.expand_path("../VERSION", File.dirname(__FILE__))) + end + + it "should expose the same version as the VERSION file" do + Mongoid::Tracking::VERSION.should == @trackoid_version + end it "should raise error when used in a class not of class Mongoid::Document" do lambda { class NotMongoidClass include Mongoid::Tracking + track :something end - }.should raise_error + }.should raise_error Mongoid::Errors::NotMongoid end - it "should not not raise when used in a class of class Mongoid::Document" do + it "should not raise error when used in a class of class Mongoid::Document" do lambda { class MongoidedDocument include Mongoid::Document include Mongoid::Tracking + track :something end }.should_not raise_error end describe "when creating a new field with stats" do @@ -44,12 +54,23 @@ it "should create a method for accesing the stats of the proper class" do @mock.visits.class.should == Mongoid::Tracking::Tracker end + it "should create an array in the class with all tracking fields" do + @mock.class.tracked_fields.should == [ :visits_data ] + end + + it "should create an array in the class with all tracking fields even when monkey patching" do + class Test + track :something_else + end + @mock.class.tracked_fields.should == [ :visits_data, :something_else_data ] + end + it "should not update stats when new record" do - lambda { @mock.inc }.should raise_error + lambda { @mock.visits.inc }.should raise_error Mongoid::Errors::ModelNotSaved end it "shold create an empty hash as the internal representation" do @mock.visits.send(:_original_hash).should == {} end @@ -64,10 +85,14 @@ it "should give today stats for last 0 days stats" do @mock.visits.last_days(0).should == [@mock.visits.today] end + it "should not be aggregated" do + @mock.aggregated?.should be_false + end + end describe "when using a model in the database" do before(:all) do @@ -164,8 +189,30 @@ # We have data for today as previous tests populated the visits field @mock.visits.on(Date.parse("2010-04-30")..Date.parse("2010-05-02")).should == [0, 10, 0] end + end + + + + context "regression test for github issues" do + + it "should not raise undefined method [] for nil:NilClass for objects already saved" do + class TestModel + include Mongoid::Document + include Mongoid::Tracking + field :name + end + TestModel.delete_all + TestModel.create(:name => "dummy") + + class TestModel + track :something + end + tm = TestModel.first + tm.something.today.should == 0 + end + end end