Wednesday, June 3, 2009

Cucumber and Subdomains

So cucumber has become my new best buddy. How can you not like this for integration testing? A brain dead monkey could get simple tests going. And that must be true, because I got some working.

In case you missed em, there's some RailsCasts (yeah, Ryan Bates is my hero) on Cucumber and Webrat here, here and here

So there I am plugging along on some tests in my latest project and just giggling like a little school girl at the simplicity of it all. Then I needed to test my subdomain routing. Brick wall. So now instead of giggling, it was more like stuck-pig-squeal. Crap.

So Google to the rescue, right? Not so fast. The problem is that cucumber is, relatively speaking, rather new, so there's not a ton of stuff up on the web yet. The other little sticky-widget I ran into was the fact that I was using 0.3.1 (which had been released like the day before) and it had changed the way some things work. So the only post I was able to find about this that contained a solution no longer worked (not to mention the fact that it was way to complicated for my lazy butt).

After struggling with this for a couple of days, I finally ran across a post that pointed me in the right direction (sorry I can't remember the site to give credit where credit is due). So I worked the kinks out and ended up with a super-simple way to test subdomains in Cucumber. So far this has worked for everything I've thrown at it.

I'd love some feedback if anyone can think of ways to improve this or sees potential problems I haven't thought out.

4 comments:

Anonymous said...

Why not just use a full url?

When I visit http://subdomain.example.com/foo

for example....

dstar

Chris Booth said...

Sorry, I should have been a little more clear. The problem isn't in the line "When I visit ...". The problem comes after that when you try to follow links on a page. It doesn't seem to follow the subdomain at that point. For example:

When I visit http://subdomain.example.com
And I log in as ...
And I follow ...

This fails (at least for me on 0.3.1) as the subsequent requests don't follow the subdomain (at least that's what I've gathered as the root of the problem).

Ramon said...

Just to clarify to others: this doesn't work with Capybara. It seems that maintaining of the host is the job of Capybara or Webrat, whichever you decide to use.

Chris Booth said...

Thanks Ramon. You're absolutely right. I just discovered this myself as I tried to move from Webrat to Capybara and all my tests started failing for my subdomain app. Still working on the solution for that one...