You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Testing Couchbase Ruby applications — unit testing with RSpec doubles, integration testing with testcontainers-ruby, scope/collection isolation
description
Testing Couchbase Ruby applications — unit testing with RSpec doubles, integration testing with testcontainers-ruby, scope/collection isolation
compatibility
Ruby SDK 3.x. RSpec 3+.
metadata
last_verified
min_server_version
handoff
2026-05
7.0
condition
skill
user asks about testing concepts or strategy
testing-patterns
condition
skill
user asks about connection setup or SDK configuration
server-connection-ruby
Testing Couchbase Ruby Applications
Unit Testing
Use RSpec doubles to mock the collection.
# Gemfilegem'rspec','~> 3.12',group: :test
RSpec.describeUserRepositorydolet(:mock_collection){double('collection')}let(:mock_result){double('get_result',content: {'name'=>'Alice'})}it'returns the correct document'doallow(mock_collection).toreceive(:get).with('user::alice').and_return(mock_result)repo=UserRepository.new(mock_collection)user=repo.get_user('user::alice')expect(user['name']).toeq('Alice')expect(mock_collection).tohave_received(:get).with('user::alice')endend
require'testcontainers'require'couchbase'RSpec.describe'OrderRepository integration',:integrationdobefore(:all)do@container=Testcontainers::DockerContainer.new('couchbase/server:7.6').with_exposed_ports(8091,8093,11210).startsleep10# allow cluster to initialiseoptions=Couchbase::Options::Cluster.newoptions.authenticate('Administrator','password')@cluster=Couchbase::Cluster.connect("couchbase://localhost:#{@container.mapped_port(11210)}",options)@collection=@cluster.bucket('default').default_collectionendafter(:all){@container.stop}it'round-trips a document'do@collection.upsert('order::1',{status: 'pending'})result=@collection.get('order::1')expect(result.content['status']).toeq('pending')endend
Scope/Collection Isolation
require'securerandom'scope_name="test_#{SecureRandom.hex(4)}"mgr=@cluster.bucket('default').collectionsmgr.create_scope(scope_name)mgr.create_collection(Couchbase::Management::CollectionSpec.new('orders',scope_name))# after testmgr.drop_scope(scope_name)