# How to test a plugin

**URL:** <https://www.ruby-forum.com/t/how-to-test-a-plugin/154727>\
**Category:** Rails\
**Created:** [December 3, 2008, 10:13am UTC](https://www.ruby-forum.com/t/how-to-test-a-plugin/154727 "2008-12-03T10:13:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![artchrst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@artchrst](https://www.ruby-forum.com/u/artchrst)\
**Post date:** [December 3, 2008, 10:13am UTC](https://www.ruby-forum.com/t/how-to-test-a-plugin/154727/1 "2008-12-03T10:13:28Z")

</div>

Guys,

I’ve created a plugin, i wanna put my automated test there.

However, I got bad file descriptor error when i run the test like normal  
unit test file:  
“ruby plugins/acts\_as\_fox/test/acts\_as\_fox\_test.rb”

How to test a plugin?

Also, is it right to say that I should only put my unit tests (but not  
functional test for example) for the plugin in the plugin test folder?

`
require File.join(File.dirname(FILE), ‘test_helper’)
require ‘test/unit’
class ActsAsTripTest < Test::Unit::TestCase
Replace this with your real tests.
def test_this_plugin
flunk
end
end
``
plugin_test_dir = File.dirname( __FILE__ )
require File.join(plugin_test_dir, '/test_helper')
Load the Rails environment
require File.join(plugin_test_dir, ‘…/…/…/…/config/environment’)
require ‘active_record/fixtures’
Load up out database & create our logger
databases = YAML::load(IO.read(plugin_test_dir + ‘/database.yml’))
ActiveRecord::Base.logger = Logger.new(plugin_test_dir + “/debug.log”)
Connect to our plugin test database
ActiveRecord::Base.establish_connection(databases[ENV[‘DB’] || ‘mysql’])
Load the test schema into the database
load(File.join(plugin_test_dir, ‘schema.rb’))
Load fixtures for the plugin
Test::Unit::TestCase.fixture_path = File.join(plugin_test_dir,
‘fixtures/’)
`

Thanks much!

Arthur

---

<div class="post-metadata">

**Author:** ![artchrst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@artchrst](https://www.ruby-forum.com/u/artchrst)\
**Post date:** [December 3, 2008, 10:24am UTC](https://www.ruby-forum.com/t/how-to-test-a-plugin/154727/2 "2008-12-03T10:24:15Z")

</div>

On Dec 3, 9:13 am, Arthur C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Guys,
> 
> I’ve created a plugin, i wanna put my automated test there.
> 
> However, I got bad file descriptor error when i run the test like normal  
> unit test file:  
> “ruby plugins/acts\_as\_fox/test/acts\_as\_fox\_test.rb”
> 
> How to test a plugin?

Well what I do is based on a skeleton plugin I have here:

> **[GitHub - fcheung/ar\_plugin\_skeleton: An empty husk of a plugin that sets up...](https://github.com/fcheung/ar_plugin_skeleton/tree/master)**
>
> master

It doesn’t do functional tests but that’s basically a case of adding

require ‘action\_controller/test\_case’  
require ‘action\_controller/test\_process’

ActionController::Routing::Routes.draw do |map|

# Install the default route as the lowest priority.

map.connect ‘:controller/:action/:id’  
end  
ActionController::Base.view\_paths = [Test::Unit::TestCase.fixture\_path

- ‘views/’]

to the test helper file

Fred

---

<div class="post-metadata">

**Author:** ![artchrst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@artchrst](https://www.ruby-forum.com/u/artchrst)\
**Post date:** [December 5, 2008, 8:16am UTC](https://www.ruby-forum.com/t/how-to-test-a-plugin/154727/3 "2008-12-05T08:16:25Z")

</div>

Thanks Fred.

It seems that the ar\_plugin\_skeleton is what I needed.

Thanks,  
Arthur

Frederick C. wrote:

> On Dec 3, 9:13ï¿½am, Arthur C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Guys,
> > 
> > I’ve created a plugin, i wanna put my automated test there.
> > 
> > However, I got bad file descriptor error when i run the test like normal  
> > unit test file:  
> > “ruby plugins/acts\_as\_fox/test/acts\_as\_fox\_test.rb”
> > 
> > How to test a plugin?
> 
> Well what I do is based on a skeleton plugin I have here:  
> [GitHub - fcheung/ar\_plugin\_skeleton: An empty husk of a plugin that sets up activerecord for its unit tests](http://github.com/fcheung/ar_plugin_skeleton/tree/master)
> 
> It doesn’t do functional tests but that’s basically a case of adding
> 
> require ‘action\_controller/test\_case’  
> require ‘action\_controller/test\_process’
> 
> ActionController::Routing::Routes.draw do |map|
> 
> # Install the default route as the lowest priority.
> 
> map.connect ‘:controller/:action/:id’  
> end  
> ActionController::Base.view\_paths = [Test::Unit::TestCase.fixture\_path
> 
> - ‘views/’]
> 
> to the test helper file
> 
> Fred

---

<div class="post-metadata">

**Author:** ![artchrst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@artchrst](https://www.ruby-forum.com/u/artchrst)\
**Post date:** [January 6, 2009, 9:57am UTC](https://www.ruby-forum.com/t/how-to-test-a-plugin/154727/4 "2009-01-06T09:57:45Z")

</div>

On Jan 6, 7:41 am, Arthur C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I can run functional test now, thanks!!
> 
> A problem I found in running the test is that it does not load the  
> route.rb so that it fails in some action — e.g. assets\_path/\_url not  
> defined.
> 
> Any clue to it is appreciated.

You need to define any routes needed in the  
ActionController::Routing::Routes.draw block in test\_helper.rb

Fred

---

<div class="post-metadata">

**Author:** ![artchrst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@artchrst](https://www.ruby-forum.com/u/artchrst)\
**Post date:** [January 6, 2009, 10:01am UTC](https://www.ruby-forum.com/t/how-to-test-a-plugin/154727/5 "2009-01-06T10:01:25Z")

</div>

Wow,

Awesome, it works now.

Thanks for the quick and useful help.

Arthur

Frederick C. wrote:

> On Jan 6, 7:41ï¿½am, Arthur C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I can run functional test now, thanks!!
> > 
> > A problem I found in running the test is that it does not load the  
> > route.rb so that it fails in some action — e.g. assets\_path/\_url not  
> > defined.
> > 
> > Any clue to it is appreciated.
> 
> You need to define any routes needed in the  
> ActionController::Routing::Routes.draw block in test\_helper.rb
> 
> Fred

---

<div class="post-metadata">

**Author:** ![artchrst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@artchrst](https://www.ruby-forum.com/u/artchrst)\
**Post date:** [January 6, 2009, 8:41am UTC](https://www.ruby-forum.com/t/how-to-test-a-plugin/154727/6 "2009-01-06T08:41:47Z")

</div>

I can run functional test now, thanks!!

A problem I found in running the test is that it does not load the  
route.rb so that it fails in some action — e.g. assets\_path/\_url not  
defined.

Any clue to it is appreciated.

Arthur

Arthur C. wrote:

> Thanks Fred.
> 
> It seems that the ar\_plugin\_skeleton is what I needed.
> 
> Thanks,  
> Arthur
> 
> Frederick C. wrote:
> 
> > On Dec 3, 9:13ï¿½am, Arthur C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > wrote:
> > 
> > > Guys,
> > > 
> > > I’ve created a plugin, i wanna put my automated test there.
> > > 
> > > However, I got bad file descriptor error when i run the test like normal  
> > > unit test file:  
> > > “ruby plugins/acts\_as\_fox/test/acts\_as\_fox\_test.rb”
> > > 
> > > How to test a plugin?
> > 
> > Well what I do is based on a skeleton plugin I have here:  
> > [GitHub - fcheung/ar\_plugin\_skeleton: An empty husk of a plugin that sets up activerecord for its unit tests](http://github.com/fcheung/ar_plugin_skeleton/tree/master)
> > 
> > It doesn’t do functional tests but that’s basically a case of adding
> > 
> > require ‘action\_controller/test\_case’  
> > require ‘action\_controller/test\_process’
> > 
> > ActionController::Routing::Routes.draw do |map|
> > 
> > # Install the default route as the lowest priority.
> > 
> > map.connect ‘:controller/:action/:id’  
> > end  
> > ActionController::Base.view\_paths = [Test::Unit::TestCase.fixture\_path
> > 
> > - ‘views/’]
> > 
> > to the test helper file
> > 
> > Fred
