Dave Schweisguth in a Bottle

How many meanings of that can you think of?

Archive for May 2011

Succinct specs for Rails named routes

leave a comment »

I like all of my routes tested, and I like all of my routes named, and I like all of my named routes tested. I even like to test my RESTful routes; even though it feels a bit like testing Rails, it more than paid for itself when migrating to Rails 3. How, then, to spec a named route succinctly?

Read the rest of this entry »

Advertisements

Written by dschweisguth

May 11, 2011 at 08:48

Posted in Programming, Rails, Ruby, Testing

Rails’ named routes considered helpful

leave a comment »

The current Rails best practice is to use RESTful routes. One nice thing about a RESTful route is that it comes with a name. Similarly, :member and :collection routes are automatically named. But sometimes you need a plain old arbitrary route, which you can then name or not as you wish. It may seem like overkill to give every route a name, especially if the route is referred to infrequently in templates. But there’s another place where you’re virtually guaranteed to need that named route again: in tests.

Read the rest of this entry »

Written by dschweisguth

May 11, 2011 at 08:36

Posted in Programming, Rails, Ruby, Testing

Do dynamic languages need more tests than static languages?

leave a comment »

Recently I made an error in a Ruby program (a Rails application) that I wouldn’t have been able to make in a Java program. I renamed a model method manually — because another class had a method with the same name, and RubyMine’s Rename Method refactoring would have renamed both methods — and although I renamed the method itself and uses of the method in the model object’s spec, I missed uses of the method in a controller and mocks of it in the controller’s spec. All of the tests passed, but the application broke. “Gee,” I thought, “if I’d had to compile everything first, that couldn’t have happened.”

Read the rest of this entry »

Written by dschweisguth

May 2, 2011 at 18:09

Posted in Programming, Testing

Avoiding extra queries in ActiveRecord 3

leave a comment »

ActiveRecord 3 introduces a new query interface based on Arel. The new query methods are more succinct than the old :conditions etc. syntax, and they’re easier to combine into complex queries. Scopes (formerly known as named scopes) are still present and useful, but there’s much less difference between a scope and a simple method that returns a query than there was before. Along with these carrots encouraging you to rewrite all your queries there is the stick of deprecation of the old syntax in Rails 3.1 and removal in Rails 3.2, so most people migrate to the new interface when they migrate to Rails 3.

Migrating GWW to the new interface went smoothly, but when I was reviewing some pages for performance recently I noticed ActiveRecord issuing some extra queries that I didn’t expect, and that weren’t necessary. Fortunately, once noticed, they were easy to eliminate.

Read the rest of this entry »

Written by dschweisguth

May 1, 2011 at 10:33

Posted in Programming, Rails, Ruby