Re: stubbing /mocking ldap within a method

Joe,

Here is the solution I decided to go with, for now:

describe Collaborator do
describe “class_methods” do
subject {Collaborator}
it { should respond_to(:search_ldap).with(1).argument }

it "should format the search term" do
  subject.prepare_search_term("Bob").should == "bob"
end

end
end

describe LdapGateway do
subject { LdapGateway.new(“surName”) }
it { should respond_to(:search_for).with(1).argument }

it “should set @ldap to an instance of Net::LDAP” do
subject.instance_eval(’@ldap’).should be_kind_of(Net::LDAP)
end

it “should set the label to the initialzied variable” do
subject.instance_eval("@label").should == “surName”
end
end

class LdapGateway
def initialize(label)
@ldap = Net::LDAP.new(credentials)
@label = label
end

def search_for(term)
@ldap.search(:filter => Net::LDAP::Filter.eq(@label, term))
end
end

class Collaborator < ActiveRecord::Base
def self.search_ldap(term)
last_name = prepare_search_term(term)
@gateway = LdapGateway.new(‘surName’)
@gateway.search_for(last_name)
# alternatively, maybe
# LdapGateway.new(‘surName’).search_for(last_name)
end

def self.prepare_search_term(term)
term.strip.split(/[, ]/).first.downcase
end
end