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.
In a typical Mongodb setup, there can be a delay between when a database write returns successfully and when that data can be read. There are two reasons for this:
- For performance gains, an "unsafe" write can return before the data is committed to the disk.
- Mongodb uses replica sets and there is a replication delay. Commonly reads are distributed to the replicas as a form of load balancing, so even if you use a safe write, you may be reading from a different server than the one you just wrote to and thus not see the data you just wrote.
To ensure that you can always immediately read back the data you just wrote using Mongoid, you need to set the database session options consistency: :strong, safe: true
, neither of which are the default.
Assuming that the problem lies in some race condition in your testing setup (and not in your code), I would advocate using rspec's expectations, which should wait for the objects to be created in the DB before counting them:
it 'should hide previous [objects] create a new updated one' do
items = MyItems.where(:type => :user_did_something)
expect { 2.times { user.log_activity(:user_did_something) } }.
to change { items.count }.from(0).to(2)
end
[edit]
To make the the test a bit cleaner overall (this wouldn't affect the behaviour though, I don't think) you could also use rspec's lazy-loading let
, like this:
let(:items_count) { MyItems.where(:type => :user_did_something).count }
it 'should hide previous [objects] create a new updated one' do
expect { 2.times { user.log_activity(:user_did_something) } }.
to change { items_count }.from(0).to(2)
end
Your test covers a lot for a unit-test:
- That
after_create
callback is called
- That
after_user_did_something
is called
- That
MyItem
object was created.
I suggest that you break it down to several unit-tests, each testing one thing. The added value you get from that is that, at least, you'll know which part of the test actually failed...
class Activity
include Mongoid::Document
after_create { |activity| my_after_create_callback(activity.event_type) }
def my_after_create_callback(activity_type)
method_name = "after_#{activity_type}"
send(method_name) if respond_to? method_name
end
def after_user_did_something
MyItem.create!(:type => :user_did_something)
end
end
it 'should call after_create' do
expect_any_instance_of(Activity).to receive(:my_after_create_callback)
.with(:user_did_something)
user.log_activity(:type => :user_did_something)
end
it 'should call the correct after activity method' do
expect_any_instance_of(Activity).to receive :after_user_did_something
user.log_activity(:type => :user_did_something)
end
it 'should create new MyItem' do
expect(MyItem).to receive(:create!).with(:type => :user_did_something)
Activity.new.after_user_did_something
end