Skip to content

Instantly share code, notes, and snippets.

@mrdanadams
Created March 28, 2012 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrdanadams/2230855 to your computer and use it in GitHub Desktop.
Save mrdanadams/2230855 to your computer and use it in GitHub Desktop.
Testing MongoDB with Mongoid, RSpec, and Rails
# do our update
@controller.record_votes(winner._id.to_s, loser._id.to_s)
# will this pass or fail? who knows!?
Item.count(conditions:{ votes:1 }).should == 0
# do our update
@controller.record_votes(winner._id.to_s, loser._id.to_s)
# wait until we get what we want (within reason, of course)
wait_until { Item.count(conditions:{ votes:1 }) > 0 }
RSpec.configure do |config|
# ...
config.before(:each) do
db = Mongoid::Config::master
# ignore stuff like system.indexes
db.collection_names.reject {|c| c =~ /^system/}.each {|c| db.drop_collection c}
end
# ...
def wait_until
i = 0
while !(result = yield) and i < 3 do
sleep 1
i += 1
end
raise "timed out" if !result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment