# Stubbing /mocking ldap within a method

**URL:** <https://www.ruby-forum.com/t/stubbing-mocking-ldap-within-a-method/208573>\
**Category:** RSpec\
**Created:** [June 21, 2011, 10:09pm UTC](https://www.ruby-forum.com/t/stubbing-mocking-ldap-within-a-method/208573 "2011-06-21T22:09:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [June 21, 2011, 10:09pm UTC](https://www.ruby-forum.com/t/stubbing-mocking-ldap-within-a-method/208573/1 "2011-06-21T22:09:39Z")

</div>

given I have the following with the internal assignments to filter and  
ldap:

class Collaborator  
def self.search\_ldap(term)  
last\_name = term.strip.split(/[,]/).first.downcase  
filter = Net::LDAP::Filter.eq(‘surName’, last\_name)  
ldap = Net::LDAP.new( :host =\> ‘foo’, :port =\> 636, :base =\> ‘bar’,  
:encryption =\> :fiz)  
ldap.search(:filter =\> filter)  
end  
end

I would like mock out the LDAP class so i can test the behavior of the  
method without LDAP.

describe Collaborator do  
it “should search ldap and return last names from the first item of  
the  
search phrase” do  
entry = Net::LDAP::Entry  
items = {:cn=\>[“Jim B.”],:uid=\>[‘bob46’],:mail=\>‘removed\_email\_address@domain.invalid’}  
items.each{|k,v| entry.stub(k) {v} }

```
# how do I get to objects inside this method, something like?
assigns(:ldap).stub(:search).and_return(entry) # but that doesnt 

```

work  
Collaborator.search\_ldap(“Bob”).should == [entry]  
end  
end

---

<div class="post-metadata">

**Author:** ![shen\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@shen\_j](https://www.ruby-forum.com/u/shen_j)\
**Post date:** [June 22, 2011, 12:01am UTC](https://www.ruby-forum.com/t/stubbing-mocking-ldap-within-a-method/208573/2 "2011-06-22T00:01:27Z")

</div>

On Tue, Jun 21, 2011 at 17:00, Jed S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I would like mock out the LDAP class so i can test the behavior of the  
> method without LDAP.

In this case, I’d invoke the rule “Don’t mock types you don’t own”.

What can you trust? LDAP does the right thing.  
What can you get wrong? Sending the wrong parameters to LDAP.  
Therefore, check the parameters you send to LDAP.

class Collaborator  
def initialize(ldapGateway = LdapGateway.new)  
@gateway = ldapGateway  
end

def self.search\_ldap(term)  
last\_name = term.strip.split(/[,]/).first.downcase  
@gateway.search(‘surName’, last\_name, ‘foo’, 636, ‘bar’, :fiz)  
end  
end

class LdapGateway  
def search(label, value, host, port, base, encryption)  
filter = Net::LDAP::Filter.eq(‘surName’, last\_name)  
ldap = Net::LDAP.new( :host =\> ‘foo’, :port =\> 636, :base =\>  
‘bar’, :encryption =\> :fiz)  
ldap.search(:filter =\> filter)  
end  
end

Judging only by the names, “label” and “value” seem separate from  
“host”“encryption”. My intuition tells me to move the latter group  
into constructor parameters.

class Collaborator  
def initialize(ldapGateway)  
@gateway = ldapGateway  
end

def self.search\_ldap(term)  
last\_name = term.strip.split(/[,]/).first.downcase  
@gateway.search(‘surName’, last\_name)  
end  
end

class LdapGateway  
def initialize(host, post, base, encryption)  
# assign to fields  
end

def search(label, value)  
filter = Net::LDAP::Filter.eq(‘surName’, last\_name)  
# SMELL Temporal duplication; can we create an LDAP in the  
constructor?  
ldap = Net::LDAP.new( :host =\> @host, :port =\> @port, :base =\>  
@base, :encryption =\> @encryption)  
ldap.search(:filter =\> filter)  
end  
end

Now the LdapGateway responds to search(label, value), so I don’t think  
Collaborator needs to know that any LDAP is going on.

class Collaborator  
def initialize(term\_repository)  
@term\_repository = term\_repository  
end

def self.search\_ldap(term)  
last\_name = term.strip.split(/[,]/).first.downcase  
@term\_repository.search(‘surName’, last\_name)  
end  
end

I’m using the mechanical name “Repository” here, because I don’t yet  
understand the role this object plays in the system.

class LdapGateway # I’m a TermRepository! Extract a module someday?  
def initialize(host, post, base, encryption)  
# assign to fields  
end

def search(label, value)  
filter = Net::LDAP::Filter.eq(‘surName’, last\_name)  
# SMELL Temporal duplication; can we create an LDAP in the  
constructor?  
ldap = Net::LDAP.new( :host =\> @host, :port =\> @port, :base =\>  
@base, :encryption =\> @encryption)  
ldap.search(:filter =\> filter)  
end  
end

Now it’s easy to check:

describe Collaborator do  
context “when searching” do  
it “should pass the proper surname to the TermRepository” do  
termRepository = mock(“a TermRepository”)  
termRepository.should\_receive(:search).with(‘surName’,  
‘rainsberger’)

```
  Collaborator.new(termRepository).search('J. B. Rainsberger')
end

it "should return the repository's results" do
  Collaborator.new(stub(:search => ["a", "b", "c"])).search('J. B.

```

Rainsberger’).should == [“a”, “b”, “c”]  
end  
end  
end

## Does that help you?

J. B. (Joe) Rainsberger :: [http://www.jbrains.ca](http://www.jbrains.ca) ::  
[http://blog.thecodewhisperer.com](http://blog.thecodewhisperer.com)  
Author, JUnit Recipes  
Free Your Mind to Do Great Work ::  
[http://www.freeyourmind-dogreatwork.com](http://www.freeyourmind-dogreatwork.com)
