Proper Extension Testing

So I am writing my first rSpec ‘tests’ formally and I’m running up
against some questions on making this work within Radiant. Can anyone
shed some light on how to spec for the following items:

* My extension should create a tab in the Admin UI.  It should be
  before or after certain other tabs.

* My extension allows the user to customize Radiant::Config items.
  If the user doesn't explicitly set these, default values should be
  used.

* I have a model with a: validates_format_of :name, etc, etc...
  declaration.  It should enforce this rule.
  Note: I've tried testing this last one by copying how Snippets are
  tested -- i.e:
      it 'should validate format of name' do
        assert_valid :name, 'abc', 'abcd-efg', 'abcd_efg',
  'abc.html', '/', '123'
        assert_invalid :name, 'cannot contain spaces or tabs'
      end
  but this does NOT seem to actually test anything.  I wrote my spec
  first and it passed without adding the 'validates_format_of' entry
  in the model.

  This might be showing a bug in the test_helper :validations code.
  I commented out the Snippet model's validates_format_of line and
  it too passed when I ran 'rake spec:models'

-Chris

Chris,

For your first question:

it ‘should insert an admin tab’ do
Radiant::AdminUI.instance.tabs.any? {|tab| tab.name == “My Tab”
}.should be_true
end

Not sure what you mean to be testing in your second question.

In your third, I would avoid using the validations test_helper. Instead
do something like this:

it ‘should validate the format of name’ do
@record.name = ‘abc’
@record.should be_valid
@record.name = ‘cannot contain spaces or tabs’
@record.should_not be_valid
@record.should have(1).errors_on(:name)
end

Sean

I would add that your spec should be more specific. When all of your
specs are generated they should be clear.
it “should validate format of name” gives you no information about
what that format should be. Your spec should describe how the
application functions, so if it were me, I’d write my spec to say:
it “has a name formatted like ‘myname’ with no spaces or tabs”
Then you go and write your regular expression for validation according
to the spec.

Great point Jim. I just stole this spec from the Radiant code (from
snippet_spec.rb). Maybe I’ll submit a patch for them too :slight_smile: .

-Chris

Man! I’m always so impressed with how “on the ball” you are. Thanks
for another super-quick reply.

As for my 2nd question, I’m trying to follow BDD principles and write
my specs before putting in any code. I was about to tackle the issue
where my extension allows the user to define configuration settings
like:
Radiant::Config[‘styles_n_scripts.javascripts_dir_name’] = ‘js’

If the extension user didn’t specify this value, elsewhere in my code
would reside a default declaration like:
Radiant::Config[‘styles_n_scripts.javascripts_dir_name’] =
‘javascripts’

Seems like something I’d want a spec or two for. I’m just not sure
whether there are any Radiant helpers/best practices for this. It
certainly doesn’t follow any kind of standard model or controller spec
that I’ve seen.

-Chris

One other thing. Is there a rake task to just run the specs for your
extension?

I see ‘rake spec:extensions’ but that runs all the extension specs
(including the built-in ones included with Radiant). Is there something
like ‘rake spec:extensions:my_extension_name’

During the development of extensions – especially if driving for BDD –
I need to be able to easily run my own extension specs constantly.

-Chris

‘autotest’ should work for testing your extensions, but it will run
constantly in the background. Otherwise, just pass
EXT=my_extension_name to rake:

rake spec:extensions EXT=my_extension_name

Sean