9

how can I use TDD with MongoDB as my second database?

Thanks

Edit:

Using Rspec or anything else that allows me to test it.

3
  • Are you just using the bare MongoDB driver, or do you have an ORM like MongoMapper, Mongoid, etc? Also, are you using MongoDB as the persistence layer for your models, for caching, files via GridFS, etc? Some extra info would be useful. Commented Apr 29, 2011 at 14:40
  • Only the ruby mongo mapper and not as the main database. No caching, nothing. However, it represents a big part of the applications which saves tons of persistent data. Commented Apr 29, 2011 at 15:23
  • You're talking about a ployglot architecture right? some models with activerecord, others as mongo documents? I can show how I do it with mongomapper, interested? Commented May 4, 2011 at 19:15

2 Answers 2

4

[Update] With MongoMapper set up you can easily use the mongodb connection directly

mongodb = MongoMapper.database
collection = mongodb.collection("my_collection")
collection.find.first
=> {"_id"=>BSON::ObjectId('4e43dfc75d1e1e0001000001'), "key1"=>"val1" }

this other SO Q/A is even more direct, using javascript functions like MongoMapper.database.eval(Mongo::Code.new('function(){ return 11 + 6; })

[/update]

I have such a polyglot architecture, some models with postgresql, others as mongo documents. I'm not really sure what you're asking, so I'll jump straight in and post most my configuration here. It includes my hacks, you probably find more beautiful config elsewhere.

I put the setup in a gist https://gist.github.com/957341

OK, so here's a document with embedded document, then the spec. I wrote the specs one by one, so they're kinda test driven.

class MyDocument
  include MongoMapper::Document
  key :title, String
  key :published_at, Time, :index => true
  key :collaborators, Array

  many :my_embedded_documents
end
class MyEmbeddedDocument
  include MongoMapper::EmbeddedDocument
  key :title, String
  key :author, String
  embedded_in :my_document
end

the spec

require "spec_helper"

describe MyDocument do

  before do
    @md = MyDocument.create(:title => "Example", :collaborators => ["mongomapper", "rspec", "oma"] )
  end
  it "should have title" do

    found = MyDocument.find(@md.id)
    found.title.should == "Example"
  end

  it "should have two my_documents" do
    MyDocument.create
    MyDocument.count.should == 2
  end

  it "should be able to fetch embedded documents" do
    @md.my_embedded_documents << MyEmbeddedDocument.new(:title => "The King", :name => "Elvis Presley")
    @md.my_embedded_documents.build(:title => "Embedded example", :name => "Embeddo")
    @md.save!
    MyDocument.where(:title => "Example").first.should == @md #findMyEmbeddedDocument.count.should == 2
  end

end

spec_helper.rb

RSpec.configure do |config|
  #...
  config.after(:each) do
    MongoMapper.database.collections.each(&:remove)
  end
end

I don't know what you wanted for answers, but I hope this will be of help to somebody.

Sign up to request clarification or add additional context in comments.

Comments

0

From what I can gather, it doesn't appear that your app is sticking to the rails MVC paradigm with its use of this secondary database that apparently doesn't store model data.

I would recommend taking the auxiliary parts of the app that depend on mongo and sticking them into a library. You can make this a gem if it makes sense to use it elsewhere. Then, create a testsuite for the logic of the library using standard testing tools, and integrate into your app either with a simple require, or some directives (depending on what it dies and how you intend to use it).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.