Saturday, November 6, 2010

Cucumber test for select box!

The behavior driven development is really fun. First of all you determine the flow of the program and then only write real codes. This technique is really effective as it saves time while programming, since the flow is already determined, the chance of hindrance is very low.

A simple test for seeing something in select box is:
Given I am on the index page
And the "Nepal" should be selected for "Country"
(where "Nepal" is your selected value and "Country" is the id/label of the select box)

The step definition for this step is
Then /^"([^\"]*)" should be selected for "([^\"]*)"$/ do |selected_value, field|
find_by_id(field).value.to_s.should == selected_value
end

Using ajax call with file field!

Sometime ago I had real trouble using file field and ajax call together. I wanted to browse a file and display the contents in the same page. Then after some research I came across gem called "remotipart". This gem is really helpful.

To use this gem follow these simple steps
1. Include "gem remotipart" in your Gemfile
2. Bundle install
3. type this in your console: rails generate remotipart
4. Include jquery-1.4.2-min.js, rails.js, jquery.form.js and jquery.remotipart.js as your javascript files
5. Suppose you are using this in you new.html.erb file, then do this
<%= form_for @product, :html => { :multipart => true }, :remote => true do |f| %>
<%= f.label :file_upload %>
<%= f.file_field :file_upload %>
<%= f.submit %>
<% end %>
6. In your create controller include: format.js {} instead of format.html{}
7. In create.js.erb file :
<%= remotipart_response do %>
//Write your javascript code here.
<% end %>

If you have any confusions go to https://github.com/formasfunction/remotipart. This site really helped me a lot.