Mocking an LDAP connection

1,319 views
Skip to first unread message

trekr5

unread,
Jan 6, 2015, 4:16:34 AM1/6/15
to ruby...@googlegroups.com
Hi,

I'm using Ruby to design an application that connects to an LDAP server.

I'm would like to develop tests to simulate my connection to the LDAP server as I don't want to connect to it every time I run my tests.

Can you suggest a way to mock/simulate this connection ?

Many thanks,


Jerry Cheung

unread,
Jan 6, 2015, 12:51:52 PM1/6/15
to ruby...@googlegroups.com
The class that handles creating the socket is Net::LDAP::Connection. It's initializer allows you to pass in a `:socket` parameter that you can use for stubbing. For an example, check out https://github.com/ruby-ldap/ruby-net-ldap/blob/ec7b5dc0e1a323e2d47e9a4e8bc84235fd56160a/test/test_ldap_connection.rb#L46-L51. See pull request #116 for more background.

angela ebirim

unread,
Jan 6, 2015, 3:33:44 PM1/6/15
to ruby...@googlegroups.com
Hi Jerry,

Thanks for the tip!



--
You received this message because you are subscribed to the Google Groups "Ruby LDAP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-ldap+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Todd

unread,
Feb 3, 2015, 7:34:18 PM2/3/15
to ruby...@googlegroups.com
To follow up on this, you may want to consider creating a mock Net::LDAP object, or stubbing out method calls (like `ldap.search`) to return an Array of `Net::LDAP::Entry` objects, depending on your needs.

For instance, with mocha:

``` ruby
ldap = Net::LDAP.new(opts)
entry = Net::LDAP::Entry.new("uid=mtodd,ou=People,dc=rubyldap,dc=com")
ldap.expects(:search).returns([entry])

ldap.search #=> [#<Net::LDAP::Entry>]
```

or with a mock object:

``` ruby
entry = Net::LDAP::Entry.new("uid=mtodd,ou=People,dc=rubyldap,dc=com")
ldap = mock("Net::LDAP", search: [entry])

ldap.search #=> [#<Net::LDAP::Entry>]
```

Cheers,
Matt
Reply all
Reply to author
Forward
0 new messages