Built-in Unit Testing
Also built into NetBeans Ruby Pack is the Ruby unit test facility called Test::Unit. From the File—>New File…menu window, you can request to create either a new Ruby Unit Test Suite or Ruby Unit Test.
A Test Suite is used to kick off the test cases. Dummy test case script calls are placed in the test suite Ruby script created by NetBeans. These get replaced with scripts you create. Given the code assistance that Ruby provides, creating a test case is a breeze because a simple control-space in the test case file gives you access to all the various test case methods to call upon (see Figure 11).
Figure 11. Code Assist for Test Cases: This can help you quickly pick the applicable assert method tests.
Figure 12. Test Suite Results: Test Cases or Test Suites are executed by running the file in NetBeans. Results are displayed in the NetBeans Output window.
To execute either a test case or a test suite, simply run the test case file or suite (Ctrl-F6). The results are displayed in the NetBeans Output window (see Figure 12).
Ruby on Rails is perhaps one of the key reasons Ruby has garnered so much attention. Due to metaprogramming generators (scripts) that build a lot of the Web site for you, most of the work in Rails revolves around executing appropriate scripts with the appropriate parameters.
When you create a new Ruby on Rails project in NetBeans, the metaprogramming generator scripts are created for you and added to the NetBeans project. Along with the necessary Rails scripts, NetBeans also create an instance of the built-in WEBrick Web server to run and test your application. Creating a Ruby on Rails application in NetBeans means using the IDE to call on the generator scripts and then adding the small amounts of glue code to make it all come together.
In order to demonstrate just how easy it is to build a Rails application using NetBeans, here are the steps to develop a small customer contact tool. If you are not familiar with standards Rails development, take a look at a past tutorial on Ruby on Rails here.
|Figure 13. Rails Generator Scripts: The many Ruby on Rails metaprogramming generator scripts that are instrumental to Rails development, are created during Rails project creation.|
First, create a Ruby on Rails application using the File—>New Project… menu option on the IDE. In the New Project window, simply select a Ruby on Rails Application and provide a project name—in this case the project is called CustomerManagement. When the finish button is pushed, NetBeans creates a project that contains all the standard Rails folders: an app folder with, controllers, helpers, models and views subfolders, a config folder, a script folder, etc. Importantly, please take a look in the script folder and note all the important rails scripts have been created (see Figure 13).
At the same time that the project and all the associated code were generated, NetBeans also set up a WEBrick server capable of running the application. In fact, right clicking on the project and selecting the Run Project menu option results in NetBeans starting the WEBrick server and launching a browser open to display the Ruby on Rails default Welcome page.
The next step is to open the database.yaml configuration file found in the Configuration folder of the project and point the application to an appropriate database. By default, Rails provides a configuration file to use a MySQL database. For this example, a small database schema called cms_development was created in MySQL. A single table was created in the schema to hold customer contact information. The SQL for that table is below.
DROP TABLE IF EXISTS `cms_development`.`contacts`;
CREATE TABLE `cms_development`.`contacts` (
`id` int(10) unsigned NOT NULL auto_increment,
`firstname` varchar(45) default NULL,
`lastname` varchar(45) default NULL,
`phone` varchar(45) default NULL,
`email` varchar(45) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Therefore, change the database.yaml
development section as shown below to give the Rails application access to the database.
Next, the Ruby on Rails model must be generated using one of the scripts provided by NetBeans. Right click on the project's Models folder and select Generate…. In the resulting Rails Generator window, enter the name Contact in the argument field to provide the name of the model to be generated. This causes Rails to create a new Ruby file in the Models
folder, called contact.rb
|Figure 14. Generating a Ruby on Rails Controller: Use the NetBeans Ruby on Rails Generator script associated to the Controllers folder to create an application controller for, in this example, the contact application.|
Now right click on the Controllers folder and select Generate… again. In the resulting Generator window, enter the name Contact again to name the application controller (see Figure 14). Leave the Views field empty.
Completing the Generator window results in the creation of a contact_controller.rb file in the Controllers folder. The file should also open in an code editor. Add one line code as shown below to add Rails scaffolding to the controller.
class ContactController < ApplicationController
And you are done! Currently, NetBeans does not have the capability to restart the WEBrick server. Again, according to the Ruby Pack Wiki site, this is a feature that will also be added in the near future. So for now, you must stop the WEBrick server and then run the project again to restart the WEBrick and the project. Once the project is running, return to the browser window where the default Welcome page is displayed. Enter the following url: http://localhost:3000/contact/list
In a matter of seconds, you have created a Web-based simple customer contact system (see Figure 15). That's the power of Ruby on Rails with NetBeans.
|Figure 15. Ruby on Rails Contact Management System via NetBeans: The results of creating a simple contact management system in only a couple of lines of code via Ruby on Rails and NetBeans.|
Almost Ready for Prime Time
NetBeans has always been a heavy and CPU-intensive tool. Minimum requirements per the NetBeans release notes page are a 500 MHz Intel Pentium III or equivalent, 512 MB of memory and 850 MB of disk space. Recommended requirements are 1.4 GHz Intel Pentium III or equivalent, 1 GB of memory and free disk space. My experience has taught me that you want to be at or better than the recommended hardware in order to reap any benefits in productivity gains that NetBeans offers—including, if not more so, NetBeans Ruby Pack.
However, overall, NetBeans is a strong IDE and that strength is now available to the Ruby community or Java developers who want to start to tap into Ruby development simplicity through JRuby. While the tool is still in development, one can expect that certain features and capabilities may still have some "quirks." Most of those should be worked out before the final release of NetBeans 6. The strength of such an IDE should help quell arguments that Ruby is not ready for industrial use because it lacks proper tool/IDE support.