Dienstag, 2. September 2008

Developing Rails Plugins with RSpec

To use RSpec while developing plugins install this rspec generator plugin as follows.

Install RSpec Plugin

script/plugin install git://github.com/pat-maddox/rspec-plugin-generator.git

Create Plugin Framework
script/generate rspec_plugin new_fu


Setup Autotesting
create a file: discover.rb to vendor/plugins/new_fu/lib/autotest/


insert the following at the top of the discover.rb file:
$:.push(File.join(File.dirname(__FILE__), %w[.. .. rspec]))
Autotest.add_discovery do
rspec
end

create a spec.opts file in the plugin/new_fu/spec folder and add the following:

--colour
--format progress
--loadby mtime
--reverse

Set Plugin Location
This avoids naming conflicts with other plugins.
create a directory structure within nuw_fu/lib somthing like this.
your_name/name_or_your_company

Now move the new_fu.rb file into the directory name_of_your_company.

Your new_fu.rb file should now be in
app/vendors/plugins/new_fu/your_name/name_of_your_company/new_fu.rb

Start Autotest for Apllication
Open a terminal window and go to the root of your apllication
first type: rake spec
It should run through and sync the app with your rspec environment
Then type: autotest
The autostest should start and re-run everytime you change a file in your application

Set test environment
open a terminal window - locate the base of your app and type:
export RAILS_ENV=test

Start Autotest for Plugin
Open a terminal window and go to the root of your plugin directory
app/vendor/plugins/new_fu/
and type in autotest
The autostest should start and re-run everytime you change a file in your plugin

Set up Plugin init.rb file.
Locate the init.rb file in vendor/plugins/new_fu
add: include 'your_name/name_of_your_company/new_fu'

Set up Plugin as a module
Open your plugin file
vendor/plugins/new_fu/your_name/name_of_your_company/new_fu.rb
add the following.

.module YourName
....module NameOfYourCompany
.......module NewFu
.
...........def hello_world
..............puts "Hello World"
...........end
.
......end
...end
.end

Then in the new_fu_spec.rb file - located in vendor/plugins/new_fu/spec/ - add the following:

.require File.dirname(__FILE__) + '/spec_helper'
.
.describe YourName::NameOfYourCompany::NewFu, "" do
......include YourName::NameOfYourCompany::NewFu
.
......before do
......end
.
......it "should should say hello world" do
........hello_world
.....end
.
.end

The autotest should have passed on the tests and you should see "hello world" somewhere in the output.

######## To Be Continued.....
Further documentation.
Understanding and building plugins - Part I
Understanding and building plugins - Part II
Creating Plugins Manual
Autotesting while writing Rails Plugins

Keine Kommentare: