ruby-on-rails-3 – TDD:Rspec Ruby MongoDB / Ruby Mongo驱动程序

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3 – TDD:Rspec Ruby MongoDB / Ruby Mongo驱动程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我怎样才能将TDD与MongoDB一起用作我的第二个数据库

谢谢

编辑:

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

解决方法

[更新]
使用MongoMapper设置,您可以直接轻松使用 mongodb连接 @H_@R_301_449@_13@mongodb = MongoMapper.database collection = mongodb.collection("my_collection") collection.find.first => {"_id"=>BSON::ObjectId('4e43dfc75d1e1e0001000001'),"key1"=>"val1" }

这个其他的SO Q / A更直接,使用像MongoMapper.database.eval(Mongo::Code.new(‘function(){ return 11 + 6; })这样的javascript函数

[/更新]

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

我把设置放在一个要点
@L_502_2@

好的,所以这是一个带有嵌入式文档的文档,然后是规范.我一个接一个地写了这些规格,所以它们有点受试验驱动.

@H_@R_301_449@_13@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

规范

@H_@R_301_449@_13@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

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

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

猜你在找的Ruby相关文章