I implemented an activity logging mechanism based on Mongoid that saves events in MongoDB.
The Mongoid model Activity
has after_create
events that perform different tasks depending on the type of activity logged: (Simplified example)
class Activity
include Mongoid::Document
after_create do |activity|
method_name = "after_#{activity.event_type}"
send(method_name) if respond_to? method_name
end
def after_user_did_something
MyItem.create!(:type => :user_did_something)
end
end
The test looks like this:
it 'should hide previous [objects] create a new updated one' do
2.times do
user.log_activity(:user_did_something)
end
items = MyItems.where(:type => :user_did_something)
items.count.should == 2
end
end
Sometimes, the tests fails on items.count
being 0 instead of 2.
This happens only when running from the command line rspec spec
it never happens when running only this test, or when running all the tests with Guard.