I’ve created a Rails “Hello World” app numerous times. But I decided to write a simple using the testing framework provided by Rails. This blog explains my experience of writing such a test. Create a “Hello World” app as: ~/samples/jruby/test >~/testbed/jruby-1.1.2/bin/jruby -S rails helloworld create create app/controllers create app/helpers create app/models create app/views/layouts create config/environments create config/initializers . . . create log/production.log create log/development.log create log/test.log There is no “-d mysql” in the command because I don’t expect this application to do any database access. The database access is disabled by following TOTD #26. I tried generating a new controller using the command: ~/samples/jruby/test/helloworld >~/testbed/jruby-1.1.2/bin/jruby script/generate controller home index and got the error: JRuby limited openssl loaded. gem install jruby-openssl for full support. https://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL /Users/arungupta/testbed/jruby-1.1.2/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:278:in `load_missing_constant’: uninitialized constant ActiveRecord (NameError) from /Users/arungupta/testbed/jruby-1.1.2/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:467:in `const_missing’ from /Users/arungupta/testbed/jruby-1.1.2/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:479:in `const_missing’ from /Users/arungupta/samples/jruby/test/helloworld/config/initializers/new_rails_defaults.rb:5:in `/Users/arungupta/samples/jruby/test/helloworld/config/initializers/new_rails_defaults.rb’ from /Users/arungupta/samples/jruby/test/helloworld/config/initializers/new_rails_defaults.rb:502:in `load’ from /Users/arungupta/testbed/jruby-1.1.2/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:502:in `load’ from /Users/arungupta/testbed/jruby-1.1.2/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:354:in `new_constants_in’ from /Users/arungupta/testbed/jruby-1.1.2/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:502:in `load’ from /Users/arungupta/testbed/jruby-1.1.2/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/initializer.rb:475:in `load_application_initializers’ … 8 levels… from /Users/arungupta/testbed/jruby-1.1.2/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/commands/generate.rb:27:in `require’ from /Users/arungupta/testbed/jruby-1.1.2/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’ from script/generate:3 So even though database access has been explicitly disabled, there are still references to ActiveRecord. So I had to explicitly disable them by changing the code in “config/initializers/new_rails_defaults.rb” as: if defined?(ActiveRecord) # Include Active Record class name as root for JSON serialized output. ActiveRecord::Base.include_root_in_json = true # Store the full class name (including module namespace) in STI type column. ActiveRecord::Base.store_full_sti_class = trueend and then the controller is easily generated as: ~/samples/jruby/test/helloworld >~/testbed/jruby-1.1.2/bin/jruby script/generate controller home index JRuby limited openssl loaded. gem install jruby-openssl for full support. https://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL exists app/controllers/ exists app/helpers/ create app/views/home exists test/functional/ create app/controllers/home_controller.rb create test/functional/home_controller_test.rb create app/helpers/home_helper.rb create app/views/home/index.html.erb Run your application using GlassFish v3 gem as: ~/workspaces/glassfish-scripting/rails/v3/src/test/rails >~/testbed/jruby-1.1.2/bin/jruby -S glassfish_rails helloworld Jun 27, 2008 2:46:18 PM com.sun.enterprise.glassfish.bootstrap.ASMain main INFO: Launching GlassFish on HK2 platform Jun 27, 2008 2:46:18 PM com.sun.enterprise.glassfish.bootstrap.ASMainHK2 findDerbyClient INFO: Cannot find javadb client jar file, jdbc driver not available Jun 27, 2008 2:46:18 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start INFO: Listening on port 3000 Jun 27, 2008 2:46:18 PM com.sun.enterprise.v3.services.impl.GrizzlyEmbeddedHttpConfigurator configureSSL WARNING: pewebcontainer.all_ssl_protocols_disabled Jun 27, 2008 2:46:18 PM com.sun.enterprise.v3.services.impl.GrizzlyEmbeddedHttpConfigurator configureSSL WARNING: pewebcontainer.all_ssl_ciphers_disabled Jun 27, 2008 2:46:19 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start INFO: Listening on port 3131 Jun 27, 2008 2:46:19 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start INFO: Listening on port 3838 Jun 27, 2008 2:46:19 PM com.sun.enterprise.v3.admin.adapter.AdminConsoleAdapter setContextRoot INFO: Admin Console Adapter: context root: /admin Jun 27, 2008 2:46:19 PM com.sun.grizzly.jruby.RailsAdapter startRubyRuntimePool INFO: Starting Rails instances Jun 27, 2008 2:46:24 PM SEVERE: JRuby limited openssl loaded. gem install jruby-openssl for full support. https://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL Jun 27, 2008 2:46:24 PM com.sun.grizzly.jruby.RubyObjectPool$1 run INFO: JRuby and Rails instance instantiation took : 5169ms Jun 27, 2008 2:46:24 PM org.glassfish.scripting.rails.RailsDeployer load INFO: Loading application helloworld at / Jun 27, 2008 2:46:24 PM com.sun.enterprise.v3.server.AppServerStartup run INFO: Glassfish v3 started in 6419 ms Jun 27, 2008 2:46:28 PM com.sun.grizzly.jruby.RailsAdapter$Logger log INFO: Chapter 9 in Rails Manual explains how to test controllers. Modify “test/functional/home_controller_test.rb” as: require ‘home_controller’ class HomeControllerTest def test_index get :index assert_response :success endend Run the test as: ~/workspaces/glassfish-scripting/rails/v3/src/test/rails/helloworld >~/testbed/jruby-1.1.2/bin/jruby -S rake test (in /Users/arungupta/workspaces/glassfish-scripting/rails/v3/src/test/rails/helloworld) /Users/arungupta/testbed/jruby-1.1.2/bin/jruby -Ilib:test “/Users/arungupta/testbed/jruby-1.1.2/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb” /Users/arungupta/testbed/jruby-1.1.2/bin/jruby -Ilib:test “/Users/arungupta/testbed/jruby-1.1.2/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb” “test/functional/home_controller_test.rb” JRuby limited openssl loaded. gem install jruby-openssl for full support. https://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL Loaded suite /Users/arungupta/testbed/jruby-1.1.2/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader Started . Finished in 0.308 seconds. 1 tests, 1 assertions, 0 failures, 0 errors /Users/arungupta/testbed/jruby-1.1.2/bin/jruby -Ilib:test “/Users/arungupta/testbed/jruby-1.1.2/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb” If you indeed are using database (which is the most common case anyway) then you can load data using Fixtures and then Test your Models. Keep adding controller and models and testing them! Please leave suggestions on other TOTD (Tip Of The Day) that you’d like to see. A complete archive is available here. Technorati: rubyonrails jruby ruby glassfish totd Ruby