[rspec-users] stubbing /mocking ldap within a method

Jed Schneider jed.schneider at gmail.com
Tue Jun 21 16:00:50 EDT 2011

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)

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 Bob"],:uid=>['bob46'],:mail=>'bob at life.edu'}
      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]
Jed Schneider
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110621/acdb7769/attachment.html>

More information about the rspec-users mailing list