Sha256: 722c855bc64bae40c30cc04e017f5341abd89562c2dcdb3f2e19f3a7152d1c08

Contents?: true

Size: 1.97 KB

Versions: 14

Compression:

Stored size: 1.97 KB

Contents

require 'spec_helper'
require 'couch_potato/rspec'

class WithStubbedView
  include CouchPotato::Persistence
  
  view :stubbed_view, :key => :x
end

describe "stubbing the db" do
  it "should replace CouchPotato.database with a stub" do
    stub_db
    CouchPotato.database.should be_a(RSpec::Mocks::Mock)
  end
  
  it "should return the stub" do
    db = stub_db
    CouchPotato.database.should == db
  end
end

describe "stubbing a view" do
  before(:each) do
    @db = stub_db
    @db.stub_view(WithStubbedView, :stubbed_view).with('123').and_return([:result])
  end
  
  it "should stub the view to return a stub" do
    WithStubbedView.stubbed_view('123').should be_a(RSpec::Mocks::Mock)
  end
  
  it "should stub the database to return fake results when called with the stub" do
    @db.view(WithStubbedView.stubbed_view('123')).should == [:result]
  end
  
  it "stubs the database to return the first fake result" do
    @db.first(WithStubbedView.stubbed_view('123')).should == :result
    @db.first!(WithStubbedView.stubbed_view('123')).should == :result
  end
  
  it "raises an error if there is no first result" do
    @db.stub_view(WithStubbedView, :stubbed_view).and_return([])
    lambda {
      @db.first!(WithStubbedView.stubbed_view('123'))
    }.should raise_error(CouchPotato::NotFound)
  end
  
  it "skips stubbing the first view (i.e. doesn't crash) if the fake result does not respond to first" do
    @db.stub_view(WithStubbedView, :stubbed_view).with('123').and_return(:results)
    
    @db.view(WithStubbedView.stubbed_view('123')).should == :results
  end
  
  it "supports the block style return syntax with `with`" do
    @db.stub_view(WithStubbedView, :stubbed_view).with('123') {:results}
    
    @db.view(WithStubbedView.stubbed_view('123')).should == :results
  end
  
  it "supports the block style return syntax without `with`" do
    @db.stub_view(WithStubbedView, :stubbed_view) {:results}
    
    @db.view(WithStubbedView.stubbed_view('123')).should == :results
  end
  
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
couch_potato-1.4.0 spec/unit/rspec_stub_db_spec.rb
couch_potato-1.3.0 spec/unit/rspec_stub_db_spec.rb
couch_potato-1.2.0 spec/unit/rspec_stub_db_spec.rb
couch_potato-1.1.4 spec/unit/rspec_stub_db_spec.rb
couch_potato-1.1.2 spec/unit/rspec_stub_db_spec.rb
couch_potato-1.1.1 spec/unit/rspec_stub_db_spec.rb
couch_potato-1.1.0 spec/unit/rspec_stub_db_spec.rb
couch_potato-1.0.1 spec/unit/rspec_stub_db_spec.rb
couch_potato-1.0.0 spec/unit/rspec_stub_db_spec.rb
couch_potato-0.7.1 spec/unit/rspec_stub_db_spec.rb
couch_potato-0.7.0 spec/unit/rspec_stub_db_spec.rb
couch_potato-0.7.0.pre.1 spec/unit/rspec_stub_db_spec.rb
couch_potato-0.6.0 spec/unit/rspec_stub_db_spec.rb
couch_potato-0.5.7 spec/unit/rspec_stub_db_spec.rb