From 5f3f73f99be47bb21f3f0145375b6847cacbc48a Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Fri, 18 Feb 2011 12:33:56 +0900 Subject: [PATCH] specs for mongoid scopes --- spec/models/mongoid_spec.rb | 52 +++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 1 + spec/support/matchers.rb | 6 +++++ 3 files changed, 59 insertions(+) create mode 100644 spec/models/mongoid_spec.rb diff --git a/spec/models/mongoid_spec.rb b/spec/models/mongoid_spec.rb new file mode 100644 index 0000000..3cbd03f --- /dev/null +++ b/spec/models/mongoid_spec.rb @@ -0,0 +1,52 @@ +require File.expand_path('../spec_helper', File.dirname(__FILE__)) +require 'mongoid' +require File.expand_path('../../lib/kaminari/mongoid_extension', File.dirname(__FILE__)) + +describe Kaminari::MongoidExtension do + before :all do + class Developer + include ::Mongoid::Document + end + end + before do + stub(subject).count { 300 } # in order to avoid DB access... + end + + describe '#page' do + context 'page 1' do + subject { Developer.page 1 } + it { should be_a Mongoid::Criteria } + its(:current_page) { should == 1 } + its(:limit_value) { should == 25 } + its(:num_pages) { should == 12 } + it { should skip(0) } + end + + context 'page 2' do + subject { Developer.page 2 } + it { should be_a Mongoid::Criteria } + its(:current_page) { should == 2 } + its(:limit_value) { should == 25 } + its(:num_pages) { should == 12 } + it { should skip 25 } + end + + context 'page "foobar"' do + subject { Developer.page 'foobar' } + it { should be_a Mongoid::Criteria } + its(:current_page) { should == 1 } + its(:limit_value) { should == 25 } + its(:num_pages) { should == 12 } + it { should skip 0 } + end + end + + describe '#per' do + subject { Developer.page(2).per(10) } + it { should be_a Mongoid::Criteria } + its(:current_page) { should == 2 } + its(:limit_value) { should == 10 } + its(:num_pages) { should == 30 } + it { should skip 10 } + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 53d69ff..cf9d527 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'rails' +require 'mongoid' require 'kaminari' require File.join(File.dirname(__FILE__), 'fake_app') diff --git a/spec/support/matchers.rb b/spec/support/matchers.rb index a353d51..4508f38 100644 --- a/spec/support/matchers.rb +++ b/spec/support/matchers.rb @@ -38,3 +38,9 @@ RSpec::Matchers.define :contain_tag_old do |count| "expected #{count || 'any'} instance(s) of #{@klass.name} but was #{@count}" end end + +RSpec::Matchers.define :skip do |num| + match do |criteria| + criteria.instance_variable_get('@options')[:skip] == num + end +end