Unit Testing HTTP cookie handling

I’m writing some cookie-handling code, and I want to write the unit
tests first. I’ve got a good idea how I’m going to do it, but I
wanted to know if there was a better way (or an example I could look
at).

I was planning on mocking up a CGI object to return my CGI::Cookie
when queried, then (somehow) causing that CGI object to be the one
that my code uses. However, I’m not very fluent in how to hack the
list of loaded modules (which I’m accustomed to doing in Perl).

Could anybody point me in a direction?

Samuel Tesla wrote:

I’m writing some cookie-handling code, and I want to write the unit
tests first. I’ve got a good idea how I’m going to do it, but I
wanted to know if there was a better way (or an example I could look
at).

I was planning on mocking up a CGI object to return my CGI::Cookie
when queried, then (somehow) causing that CGI object to be the one
that my code uses. However, I’m not very fluent in how to hack the
list of loaded modules (which I’m accustomed to doing in Perl).

Could anybody point me in a direction?

One thing you can do is to setup the code which you’re unit testing to
accept a cgi object as a parameter. Then, in your tests you can
instantiate a CGI object, put the cookies you want into it and then hand
it off to the code that you’re testing. Of course, if all that code
does is handle cookies, you don’t even need a CGI object. You can pass
in any object that responds to “cookies” with a Hash-like thing. Ruby
is nice for doing mock objects, since all you have to do is respond to
the right messages (but not worry about object type).

Chad

Chad Fowler chadfowler@chadfowler.com writes:

One thing you can do is to setup the code which you’re unit testing to
accept a cgi object as a parameter. Then, in your tests you can
instantiate a CGI object, put the cookies you want into it and then hand
it off to the code that you’re testing. Of course, if all that code
does is handle cookies, you don’t even need a CGI object. You can pass
in any object that responds to “cookies” with a Hash-like thing. Ruby
is nice for doing mock objects, since all you have to do is respond to
the right messages (but not worry about object type).

I was thinking about doing that. What I’ve settled on, though, is
using WebUnit. Since I’m actually writing a full out CGI app, and
WebUnit looks pretty nice.

Thanks,
Samuel

Samuel Tesla samuel@alieniloquent.com writes:

Chad Fowler chadfowler@chadfowler.com writes:

One thing you can do is to setup the code which you’re unit testing to
accept a cgi object as a parameter. Then, in your tests you can
instantiate a CGI object, put the cookies you want into it and then hand
it off to the code that you’re testing. Of course, if all that code
does is handle cookies, you don’t even need a CGI object. You can pass
in any object that responds to “cookies” with a Hash-like thing. Ruby
is nice for doing mock objects, since all you have to do is respond to
the right messages (but not worry about object type).

I was thinking about doing that. What I’ve settled on, though, is
using WebUnit. Since I’m actually writing a full out CGI app, and
WebUnit looks pretty nice.

Well, WebUnit would look pretty nice. For some reason I can’t get it
to play properly with Test::Unit the way my other unit tests do. I’ve
opted to just fake the CGI object after all…it’s much simpler.

Any suggestions on getting WebUnit to play with Test::Unit…from the
looks of it, it needs a major overhaul to work with the new way of
doing things. Perhaps subclass it from Test::Unit::TestCase?