开发者

TDD: Rspec Ruby MongoDB/Ruby Mongo Driver

开发者 https://www.devze.com 2023-02-28 17:44 出处:网络
how can I use TDD with MongoDB as my second database? Thanks Edit: Using Rspec or anything else that a开发者_StackOverflowllows me to test it.[Update]

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

Thanks

Edit:

Using Rspec or anything else that a开发者_StackOverflowllows me to test it.


[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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消