# Full plugin test environment (long)

**URL:** <https://www.ruby-forum.com/t/full-plugin-test-environment-long/93218>\
**Category:** Rails\
**Created:** [April 29, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/full-plugin-test-environment-long/93218 "2007-04-29T14:57:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cinek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@cinek](https://www.ruby-forum.com/u/cinek)\
**Post date:** [April 29, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/full-plugin-test-environment-long/93218/1 "2007-04-29T14:57:50Z")

</div>

Testing part of rails framework works very nice. Unfortunately testing  
plugins seems to be a pain (at least for me).

I’m writing a simple plugin that adds a model and a controller mixin (it  
seems to work fine in my application). Now I’d like to write tests for  
it. They’ll require at least a controller (that can be mixed into) and  
database (probably no fixtures).

I’d also like the test to be independent of any applications, so I  
thought that using in-memory sqlite db would be quite nice.

Based on tests of plugin\_dependencies and plugin\_migrations plugins I’ve  
managed to create something like this in my plugin’s test subdirectory:

rails\_root/  
rails\_root/config/  
rails\_root/config/boot.rb  
rails\_root/config/database.yml  
rails\_root/config/environment.rb  
rails\_root/config/environments/in\_memory.rb  
unit/  
unit/stored\_action\_test.rb  
test\_helper.rb

The file boot.rb has been copied from main rails-app config directory.  
environment.rb just requires boot.rb. in\_memory.rb file is empty.

database.yml defines an sqlite :memory: adapter.

test\_helper.rb seems to be the most important file here, this is its  
contents:

require ‘rubygems’

ENV[‘RAILS\_ENV’] ||= ‘in\_memory’

# Load test unit

require ‘test/unit’

# Load rails (TODO: there must be a better way)

require ‘active\_record’  
require ‘active\_record/base’  
require ‘active\_record/fixtures’  
require ‘action\_controller’

# Load the plugin

#$LOAD\_PATH \<\< File.join(File.dirname( **FILE** ), ‘…’, ‘lib’)  
require File.join(File.dirname( **FILE** ), ‘…’, ‘init’)

There is also one unit test (stored\_action\_test.rb):

require File.dirname( **FILE** ) + ‘/…/test\_helper’

class StoredActionTest \< Test::Unit::TestCase

def test\_key  
puts “Test 1”  
stored\_action = StoredAction.new(:action =\> ‘test’)  
puts “Test 2”  
assert(stored\_action.save)  
key = stored\_action.key  
end  
end

Running “rake test” from plugin’s main directory gives me this:

cinek [14:47] vendor/plugins/action\_store \> rake test  
(in /home/cinek/src/gallery/vendor/plugins/action\_store)  
/usr/local/bin/ruby18 -Ilib:lib  
“/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake/rake\_test\_loader.rb”  
“test/functional/action\_store\_test.rb” “test/unit/stored\_action\_test.rb”  
Loaded suite  
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake/rake\_test\_loader  
Started  
FTest 1

The test clearly fails but it gives me no idea why creation of  
StoredAction doesn’t succeed (it is the model that the plugin supplies.  
It works fine within the application).

So I have a question: what is the proper way of creating test  
environment to:

- get some more information in cases like this,
- not have to require every bit of rails that is used by tests by hand  
in test\_helper.rb,
- create schema.rb for the tests (I haven’t tried doing this yet),
- that would not require in-depth knowledge of rails implementation (I  
don’t believe in that one :).

I know that there may be some issues with in-memory sqlite3 database,  
but I guess I haven’t reached that point yet.

Sorry for the long mail, but since I’ve got this far (and it took me a  
lot of time 🙂 I thought that it might be easier for you to help me  
carry on from this point than from the beginning.

I have found a quite a few blog entries on the net that scratch the  
surface, but it’s difficult for me to put these tiny pieces together  
into something meaningful :-/

## Thanks in advance!

Marcin S.

---

<div class="post-metadata">

**Author:** ![cinek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@cinek](https://www.ruby-forum.com/u/cinek)\
**Post date:** [April 29, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/full-plugin-test-environment-long/93218/2 "2007-04-29T15:20:58Z")

</div>

Marcin,

Is it worth a hassle? We write lots of plugin all covered with tests.  
We don’t include tests with plugins though but each plugin development  
tree has its own rails application just for testing the plugin. For  
example, to install our ActsAsSecure plugin, you run ‘script/plugin  
install  
svn://rubyforge.org/var/svn/acts-as-secure/trunk/vendor/plugins/acts\_as\_secure’.  
As you can see it is located under vendor/plugin while the tests are  
under svn://rubyforge.org/var/svn/acts-as-secure/trunk/test. It does  
not give others an ability to run tests for your plugin (unless they  
check out the whole app just for that) but it makes it much easier for  
you to test it during development.

Regards,  
Val

[http://revolutiononrails.blogspot.com/](http://revolutiononrails.blogspot.com/)

On Apr 29, 8:57 am, Marcin S. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![cinek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@cinek](https://www.ruby-forum.com/u/cinek)\
**Post date:** [April 29, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/full-plugin-test-environment-long/93218/3 "2007-04-29T20:22:18Z")

</div>

Val wrote:

> Is it worth a hassle? We write lots of plugin all covered with tests.  
> We don’t include tests with plugins though but each plugin development  
> tree has its own rails application just for testing the plugin. For

Yes, this should be a lot easier. The funny thing is that I remember  
thinking about this approach 🙂  
For the record: my test above fails for some unknown reason, even when  
tested in its own application.

## Anyway, I found this, which is the answer to my question: [http://www.pluginaweek.org/2006/11/24/plugin-tip-of-the-week-testing-your-plugins-the-right-way/](http://www.pluginaweek.org/2006/11/24/plugin-tip-of-the-week-testing-your-plugins-the-right-way/)

Marcin S.
