Thursday, February 10, 2011
Monday, January 18, 2010
I initially tried to use HttpMock which is used to test Active Resource framework within Rails. It is undocumented and you need to dig through web and Rails documentation it self to see how it works. Recently I came across excellent FakeWeb gem and ever since that I have been hooked on it. I am impressed by simplicity with which we I can mock RESTFul Rails web services.
Here is how I use Test::Unit and Shoulda with Rails to Mock RESTful web services for Active Resource classes. I am using XML based web services so I needed sample XML responses that Mock service should return. I create XML files containing those sample responses and store them under test/fixtures folder. After that I edited test_helper.rb file in test folder to have this utility method which loads sample responses from XML files located in fixtures folder.
After this lets jump on to unit test that we are going to write for our Active Resource class. Lets say we are testing an Active Resource class named User. Here is how I write unit test with FakeWeb and Shoulda gem.
First I am requiring both FakeWeb and Shoulda a gem. I would also add these gems to config/environments/test.rb as required gem for test environment.
After requiring these two gems I would disable real HTTP connections so not to mix real Web services calls with mock RESTful Web services by setting allow_net_connect property of FakeWeb class to false. Similarly in teardown method I would re-enable net HTTP connections by setting allow_net_connect property to true.
Now I am going to write actual test using FakeWeb within Shoulda context blocks.
Here within the context block I am setting up url to register as GET request to return response that is contained in XML file. After that a simple test checks the returned response object with some assertions. This shows how easy it is to create mock RESTful web services with FakeWeb and test your Active Resource classes with RSpec or Test::Unit
Thursday, October 1, 2009
Rails application. Plugin assumes existance of model named User and that model
contains information about users of system. If User model does not exist the
plugin will print warning message on console at startup. If you dont already
have authentication user model i recommend you install restful_authentication
The plugin uses login attribute of user model in views by default. You can
change that default by declaring this constant in your environment.rb file
VIEW_QA_USER_ATTRIBUTE = "full_name"
To change look of the module change the css refrenced in
Install this plugin in your rails application by running
./script/plugin install git://github.com/hasham2/questions_answers_foo.git
To complete installation of plugin run following rake task from root directory
of your application.
This will create database migration to hold Questions and Answers data. After
running this task run:
to commit changes to database structure.
Monday, May 11, 2009
To make it even better is that it comes with built in support for number of version control systems like Darcs, Subversion, Mercurial and best of all Git. To get started you need to install vlad gem on your machine and on each of servers you wish to deploy your ruby app.
sudo gem install vlad
The variables for deployment are typically setup in config/deploy.rb file just like capistrano. For trival one server deployment all you have to do is set four vaiables in deploy.rb file
set :application, "myblog"
set :domain, "blog.com"
set :deploy_to, "/var/rails/myblog"
set :repository", "http://svn.myserver.com/myblog/trunk"
Also add following lines to Rakefile as well to require vlad gem
Just run following commands in this sequence to deploy your application
vlad:setup #run first time only
As you might be aware Capistrano is not going to be maintained anymore by Jamis buck and there is lot of active development going on for vlad it should be good idea to give vlad a spin as alternative to capistrano.
Tuesday, March 10, 2009
Test::Unit which is a default unit testing framework with rails. It has only one context for each test case class. If we can have more than one context per class than it could reduce number of files and classes that we are required to make to test cases in multiple contexts.
Answer to this issue is support for multiple contexts in Shoulda. Shoulda is rspec like testing framework built upon Test::Unit. Shoulda allows you to have multiple context with thier own setup and teardown methods. For example see this context block in a test case
class UserTest < Test::Unit::TestCase
context "with user" do
@user = User.create(:login => "john", :password => "secret" )
should "be able to authenticate" do
assert User.authenticate("john", "secret")
should "return authenticated user" do
assert_equal @user, User.authenticate("john", "secret")
For more details on shoulda testing framework take a look at official rails wiki page
Tuesday, July 15, 2008
yum install rubyyum install irbyum install rdocyum install ruby-develyum install rubygems
gem install rails -y
yum install httpd-devel
gem install passenger
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.1/ext/apache2/mod_passenger.soPassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.1PassengerRuby /usr/bin/ruby
<VirtualHost *:80>ServerName www.demo.comDocumentRoot /var/www/html/myapp/public</VirtualHost>
service httpd start