Webrat is library to write acceptance tests for ruby web applications. I also like shoulda for its context. However using the two in conjunction has been little fun. I wanted to multiple sessions testing. Following is the code I used to get it done using shoulda and webrat. It might turn out to be useful example for some.

require File.dirname(__FILE__) + '/../test_helper'

class UserStories  'some@one.com'
      fill_in "password", :with => "idunno"
      click_button
      assert_equal '/session', path
    end
  end

  context 'A User' do
    setup do 
      visit "login"
      assert_response :success
      fill_in "email",:with => users(:tom).email
      fill_in "password", :with => "123456"
      click_button
      assert_equal '/profile', path
    end

    should 'be able to login with valid email and password' do
      visit 'forums/new'
      assert_response :success
    end
  end

end

For an assignment that I am working on I was entangled in unicode mess. Atleast now I understand it little better. One of the problems that was to be solved was to determine default character set of mysql database.

Here is what I did to extract it.

SELECT SCHEMA_NAME AS `Database`, DEFAULT_CHARACTER_SET_NAME   FROM INFORMATION_SCHEMA.SCHEMATA;

This  was grabbed from the information here MySQL Docs.

There is this small rails tip that helped me customize the header of validation errors in rails.

I use STI a lot in my projects and I have base class of Post from which News, Video inherit. The error shown in case the validation failed for video was

4 errors prohibited this post from being saved

There were problems with the following fields:

  • Title can’t be blank
  • Url can’t be blank
  • Url is invalid
  • Description can’t be blank
Looks like rails makes use of table names to define the header message. I just used the following code to change the header message.
class News < Post
  ...
  def pretty_type
    'News'
  end
end

class Video < Post
  ...
  def pretty_type
    'Video'
  end
end

# in my view code where @post is instance of video or news, I customize form error message as below
f.error_messages :header_message => "#{@post.errors.length} errors prohibited #{@post.pretty_type} from being saved."

That was pretty easy.

Rails infers the name of controller under test from the name of testcase. Most of the times it works correctly and there are times when it does not following is the controller testcase where it did not work

require File.dirname(__FILE__) + '/../test_helper'

class OrganizaionUsersControllerTest < ActionController::TestCase

 ...

end

There is classmethod by which you can explicitly tell the testcase which controller you are testing. It is called test following is the example usage.


require File.dirname(__FILE__) + '/../test_helper'

class OrganizaionUsersControllerTest < ActionController::TestCase
    tests OrganizationUsersController
 ...
end

Working on one of the pet projects, which I usually do in python. I got this error

RuntimeError ‘maximum recursion depth exceeded’

I have seen that error at some points. I was working with sphinx and may be this error was caused by docutils traversing its doctrees. I remember having solved it using code like this


import sys
LimitForApplication = 2000
sys.setrecursionlimit(LimitForApplication)

Default recursion limit for python is 1000. You can set recursion limit for your application as you wish.

Just to fit it in my head and to make a note that ‘svn revert’ and ‘git revert’ are not same. I have fallen into it couple of times. To know more http://bryan-murdock.blogspot.com/2007/07/git-revert-is-not-equivalent-to-svn.html

I tried to setup the project on different machine and one of the shoulda macros did not work. It was due to different packaging of macros with new shoulda gem. I have at times placed the gems into vendor manually, but here was gemsonrails which made managing gems much easier.

Sphinx generates documentation from reStructuredText. Sphinx uses reStructuredText as its markup language, and many of its strengths come from the power and straightforwardness of reStructuredText and its parsing and translating suite, the Docutils.

Currently I am working on a project which would involve writing an extension. This extension would be written as a builder which would output the restructured text into custom html format. I am on a lookout for some good examples to develop an extension. Hope the community guides me in the right direction. More of sphinx discussions soon.

Its been a while since I have been using shoulda and its pretty awesome addition to your rails TDD artilery. Of what little I have known of  it has helped me to write my own first shoulda macro.

Here it is

module ThoughtBot
  module Shoulda
    module Controller
      module Macros
        # :section test macro
        # macro creating test asserting that actions requiring login are redirected to login
        def should_request_login(action, params = {}, method = :get)
          context "#{method.to_s.upcase} on #{action.to_s} " do 
            setup do 
              case method
              when :get
                get action, params
              when :post 
                post action, params
              when :put
                put action, params
              when :delete
                delete action, params
              end
            end
            should "should redirect to login" do
              assert_response :redirect
              assert_redirected_to new_session_path
            end
          end
        end
      end
    end
  end
end

I am working on few more. May be I may post them sometime later.

After every migration you add to your rails app and if you are using TDD to develop your app, remember to update your test database to the recent schema changes.

Use the following to get it done.

rake db:test:prepare

Next Page »



Follow

Get every new post delivered to your Inbox.