TDD:Rspec Ruby MongoDB/Ruby Mongo驱动程序

don*_*ald 8 tdd mongodb rspec2 ruby-on-rails-3

我怎样才能将TDD与MongoDB一起用作我的第二个数据库?

谢谢

编辑:

使用Rspec或其他允许我测试它的东西.

oma*_*oma 4

[更新] 设置MongoMapper后,您可以轻松地直接使用mongodb连接

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

另一个 SO Q/A 甚至更直接,使用 JavaScript 函数,如MongoMapper.database.eval(Mongo::Code.new('function(){ return 11 + 6; })

[/更新]

我有这样的多语言架构,一些模型使用 postgresql,其他模型作为 mongo 文档。我不太确定你在问什么,所以我会直接跳进去并在这里发布我的大部分配置。它包括我的技巧,您可能会在其他地方找到更漂亮的配置。

我将设置放在要点中 https://gist.github.com/957341

好的,这是一个包含嵌入文档的文档,然后是规范。我一一写了规格,所以它们是经过测试的。

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
Run Code Online (Sandbox Code Playgroud)

规格

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
Run Code Online (Sandbox Code Playgroud)

规范助手.rb

RSpec.configure do |config|
  #...
  config.after(:each) do
    MongoMapper.database.collections.each(&:remove)
  end
end
Run Code Online (Sandbox Code Playgroud)

我不知道你想要什么答案,但我希望这对某人有帮助。