spec/cursor_spec.rb in mongodoc-0.2.1 vs spec/cursor_spec.rb in mongodoc-0.2.2

- old
+ new

@@ -1,85 +1,91 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe "MongoDoc::Cursor" do - before do - @mongo_cursor = stub('cursor') - @cursor = MongoDoc::Cursor.new(@mongo_cursor) - end + let(:mongo_cursor) { stub('cursor') } + let(:collection) { stub('collection') } + + let(:cursor) { MongoDoc::Cursor.new(collection, mongo_cursor) } + it "is Enumerable" do - Enumerable.should === @cursor + Enumerable.should === cursor end it ".new wraps a Mongo::Cursor" do - @cursor._cursor.should == @mongo_cursor + cursor._cursor.should == mongo_cursor end + it "#collection returns the MongoDoc::Collection for this cursor" do + cursor.collection.should == collection + cursor._collection.should == collection + end + context "with the underlying cursor" do - %w(close closed? count explain limit query_options_hash query_opts skip sort).each do |delegated_method| + %w(admin close closed? count explain fields full_collection_name hint limit order query_options_hash query_opts selector skip snapshot sort timeout).each do |delegated_method| it "delegates #{delegated_method} to the Mongo::Cursor" do - @mongo_cursor.should_receive(delegated_method) - @cursor.send(delegated_method) + mongo_cursor.should_receive(delegated_method) + cursor.send(delegated_method) end end end context "#each" do it "delegates to the cursor" do - @mongo_cursor.should_receive(:each) - @cursor.each + mongo_cursor.should_receive(:each) + cursor.each end it "decodes the return from the delegate" do bson = stub('bson') - @cursor.stub(:_cursor).and_return([bson]) + cursor.stub(:_cursor).and_return([bson]) MongoDoc::BSON.should_receive(:decode).with(bson) - @cursor.each {} + cursor.each {} end it "calls the block with the decoded return" do result = stub('bson') - @cursor.stub(:_cursor).and_return([result]) + cursor.stub(:_cursor).and_return([result]) MongoDoc::BSON.stub(:decode).and_return(result) - @cursor.each {|obj| @obj = obj} + cursor.each {|obj| @obj = obj} @obj.should == result end end context "#next_document" do it "delegates to the cursor" do - @mongo_cursor.should_receive(:next_document) - @cursor.next_document + mongo_cursor.should_receive(:next_document) + cursor.next_document end it "decodes the return from the delegate" do bson = stub('bson') - @mongo_cursor.stub(:next_document).and_return(bson) + mongo_cursor.stub(:next_document).and_return(bson) MongoDoc::BSON.should_receive(:decode).with(bson) - @cursor.next_document + cursor.next_document end it "returns nil if the delegate returns nil" do - @mongo_cursor.stub(:next_document) - @cursor.next_document.should be_nil + mongo_cursor.stub(:next_document) + cursor.next_document.should be_nil end end context "#to_a" do it "delegates to the cursor" do - @mongo_cursor.should_receive(:to_a) - @cursor.to_a + mongo_cursor.should_receive(:to_a) + cursor.to_a end it "decodes the return from the delegate" do array = stub('array') - @mongo_cursor.stub(:to_a).and_return(array) + mongo_cursor.stub(:to_a).and_return(array) MongoDoc::BSON.should_receive(:decode).with(array) - @cursor.to_a + cursor.to_a end it "returns [] if the delegate returns []" do - @mongo_cursor.stub(:to_a).and_return([]) - @cursor.to_a.should == [] + mongo_cursor.stub(:to_a).and_return([]) + cursor.to_a.should == [] end end end