# Proper Extension Testing

**URL:** <https://www.ruby-forum.com/t/proper-extension-testing/136175>\
**Category:** Radiant CMS\
**Created:** [April 29, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/proper-extension-testing/136175 "2008-04-29T17:02:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 29, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/proper-extension-testing/136175/1 "2008-04-29T17:02:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 29, 2008, 5:11pm UTC](https://www.ruby-forum.com/t/proper-extension-testing/136175/2 "2008-04-29T17:11:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 29, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/proper-extension-testing/136175/3 "2008-04-29T17:43:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 29, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/proper-extension-testing/136175/4 "2008-04-29T19:16:10Z")

</div>

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 🙂 .

-Chris

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 29, 2008, 7:20pm UTC](https://www.ruby-forum.com/t/proper-extension-testing/136175/5 "2008-04-29T19:20:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 29, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/proper-extension-testing/136175/6 "2008-04-29T19:28:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 29, 2008, 7:52pm UTC](https://www.ruby-forum.com/t/proper-extension-testing/136175/7 "2008-04-29T19:52:36Z")

</div>

‘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
