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

Jed Schneider jed.schneider at gmail.com
Wed Jun 22 16:33:43 EDT 2011


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
-- 
Jed Schneider
706-207-4458 (m)
803-386-7214
jedschneider.posterous.com
twitter.com/jedschneider
github.com/jedschneider
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110622/5544e7d2/attachment.html>


More information about the rspec-users mailing list