Panos Matsinopoulos

Passionate software reader, writer, devops builder and classical music lover.

Testing - Asserting Template and Layout

| Comments

I consider testing one of the most important phase in application development and Rails does a very good job on that. However, testing documentation on Rails Guides is still work under development.

Here is a short tutorial on how you can test that a reponse has rendered the correct template and the correct layout.

If you want to make sure that the response rendered the correct template and layout, you can use the assert_template method:

assert_template usage
1
2
3
4
5
test "index should render correct template and layout" do
  get :index
  assert_template :index
  assert_template :layout => "layouts/application"
end

Note that you cannot test for template and layout at the same time, with one call to assert_template method. Also, for the layout test, you can give a regular expression instead of a string, but using the string, makes things clearer. On the other hand, you have to include the “layouts” directory name even if you save your layout file in this standard layout directory. Hence,

This will not work
1
assert_template :layout => "application"

will not work.

Gotcha: Watch out if your view renders any partial

If your view renders any partial, when asserting for the layout, you have to assert for the partial at the same time. Otherwise, assertion will fail.

Hence:

Correct way to assert for the layout
1
2
3
4
test "new should render correct layout" do
  get :new
  assert_template :layout => "layouts/application", :partial => "_form"
end

is the correct way to assert for the layout when the view renders a partial with name_form. Omitting the :partial key in your assert_template call will complain.

Ruby Style Guide

| Comments

This is a very good guide on how one should style its Ruby code. I am following this since I started Ruby and I suggest that everybody does the same.

One extra on usage of {…} vs do…end blocks can be found here (Weirich Convention).

Maze Implementaion in Ruby

| Comments

This is an implementation of a Maze algorithm to find the path from start to goal in between the walls of a maze.

Read the details here.

Octopus Problem With Rake

| Comments

This week I have decided to use octopus, a fantastic gem for realizing replication or sharding. I have already implemented MySQL Replication (Master / Slave) and I wanted to have my Ruby on Rails application actually use it!

This is my github project: Octopus Replication Example

Your comments are welcome!

Rails and Test Unit Bug on Counting Passed Tests

| Comments

During the weekend I have discovered a bug on Rails activesupport gem. The truth is that it has already been reported to Rails. It is the issue https://github.com/rails/rails/pull/2472.

You can see my detailed answer here: http://stackoverflow.com/questions/7706487/rails-tests-showing-0-passed-when-all-pass/7939497#7939497

Use the gist that is referred there until the bug is fixed.

As always, your comments are welcome.

Rails Validator Attachment

| Comments

I believe that testing is the most important part of the software development lifecycle. Nothing is ready unless it passes successfully all tests.

Meanwhile, I am trying to test as much aspects of my Rails application as possible. One such are the validations that I have on my models. To help my task on testing validations, I have created a gem that helps me out there.

You can find more details here:

Validator Attachment

Your comments are welcome.

Ruby on Rails Gems I Use

| Comments

Since I have started developing applications with Ruby on Rails, I have used again and again some very useful gems. Here they are:

  • bundler: The gem dependency resolver and manager.
  • mysql2: The ActiveRecord adapter for MySQL. MySQL is my personal preference for database server.
  • fast_gettext, gettext_i18n_rails, gettext: For I18n.
  • routing-filter: I used this one in one of my multilingual applications, when I wanted my paths to include the locale.
  • foreigner: Allows me to declare foreign keys in my migrations.
  • nilify_blanks: Very useful to convert empty parameter values to nulls and avoid storing empty strings in db where it should be null.
  • jquery-rails: For jQuery used in rails.
  • settingslogic: For run-time configuration settings.
  • dynamic_form: For “error_messages_for” in my views.
  • kaminari: For pagination in my index views.
  • unicode: For Unicode support which is missing in ruby 1.8 (available in 1.9).
  • rmagick, carrierwave: For image uploading and resizing.
  • acts_as_list: For lists that I want to be able to easily reposition their items.
  • sitemap_generator: Usefull gem to let you generate sitemap files.

Especially for development and testing:

  • mongrel: A web server adequate for development and better than Webrick.
  • web-app-theme: A layout for quick start of my web fron end.
  • ruby-debug, ruby-debug-ide: For debugging from within my IDE (Intellij)
  • mocha: For Moching and Stubbing
  • single_test: To be able to run a single test from the command line
  • cucumber-rails, database_cleaner: For Cucumber tests.