# \[ANN\] ActiveSpec - Ruby Specifications library (preview)

**URL:** <https://www.ruby-forum.com/t/ann-activespec-ruby-specifications-library-preview/69418>\
**Category:** Rails\
**Created:** [September 20, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/ann-activespec-ruby-specifications-library-preview/69418 "2006-09-20T22:42:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 20, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/ann-activespec-ruby-specifications-library-preview/69418/1 "2006-09-20T22:42:00Z")

</div>

Hi Guys

Over the past week (well, since RailsConf finished) I’ve been working  
on a Ruby Specifications library called ActiveSpec, with the aim to  
provide an alternative to Rails validations module (though ActiveSpec  
can be used outside of Rails apps too).

What is ActiveSpec?

It is an implementation of the Specification domain pattern. ActiveSpec  
provides built-in low-level specification classes (that mostly mirror  
Rails’ validates\_\* macros) and a few extras like composite specs and a  
spec negator decorator. It provides a more declarative way of creating  
composite specs, with the ActiveSpec::Base class, and finally it  
provides a high-level DSL for creating specifications. What does that  
DSL look like?

specification :user do  
should\_require\_presence\_of :username, :password  
should\_require\_confirmation\_of :password  
end

You can also do a form of specification inheritance:

specification :authenticated\_user do  
should\_satisfy :user\_specification # the above spec  
end

You can even uses Ruby procs for quick, arbitrary specs:

specification :foo do  
should\_satisfy { |obj| # do something }  
end

Finally there is the Satisfies module which makes attaching specs  
directly to your classes (including ActiveRecord classes), a breeze:

ActiveRecord::Base.send(:include, ActiveSpec::Satisfies)

class User \< ActiveRecord::Base  
should\_satisfy :user\_specification  
should\_satisfy :activated\_user\_specification, :if =\> :activated?  
end

Why ActiveSpec?

Rails’ validates\_\* macros are awesome but sometimes you want a bit more  
power, and using the lower level validate methods is a bit icky. More  
importantly, as we found out on a recent project with a particularly  
complex domain model, you sometimes want to validate your model in  
different ways depending on its state or what context it is being used  
in. Its hard to do this elegantly with Rails’ current validations API.

Is it ready to use?

Not quite, but theres enough there to check out and start playing with.  
The major thing that it is lacking that Rails validations have, is  
error messages. This _will_ be added but I haven’t tackled it yet. But  
I’d love people to check out the trunk and have a play and provide me  
with some feedback at this early stage.

Where can I find it?

You can grab the source code from the Agile Evolved open source  
repository:  
[http://opensource.agileevolved.com/svn/root/activespec/trunk/](http://opensource.agileevolved.com/svn/root/activespec/trunk/)

You can check out the RDocs here:  
[http://docs.britsonrails.org.uk/activespec/](http://docs.britsonrails.org.uk/activespec/)

I’ve tried to fully document the API as best as I can. There’s also a  
README file there with more information.

License?

MIT, natch!

Have fun, let me know how you get on!

(by the way, if you want to know more about the Specification pattern  
beyond what google tells you, I cannot recommend Domain Driven Design  
by Eric Evans enough!).

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 20, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/ann-activespec-ruby-specifications-library-preview/69418/2 "2006-09-20T22:44:00Z")

</div>

Hooray…finally a post of mine shows up on the Rails google group. 🙂

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 21, 2006, 11:05am UTC](https://www.ruby-forum.com/t/ann-activespec-ruby-specifications-library-preview/69418/3 "2006-09-21T11:05:12Z")

</div>

On Wednesday 20 September 2006 22:34, removed\_email\_address@domain.invalid wrote:  
[snip]

> specification :user do  
> should\_require\_presence\_of :username, :password  
> should\_require\_confirmation\_of :password  
> end  
> [snip]  
> class User \< ActiveRecord::Base  
> should\_satisfy :user\_specification  
> should\_satisfy :activated\_user\_specification, :if =\> :activated?  
> end

I think the terminology is rather odd. It is apparently inspired by  
behavio(u)r-driven design, but goes over board with it. BDD makes sense  
in an executable specification where you state what a thing is  
intended – “should” – to do and check it at the same time. But in the  
thing itself you state what it does do.

When I read code such as the excerpts above, I stumble. Hey, this User  
class should satisfy some specification – but does it really? What I  
expect instead is

satisfies :user\_specification

Then I happily leave it to the tests to determine whether this claim is  
true and the class really satisfies the specifications it, well,  
should.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 21, 2006, 2:35pm UTC](https://www.ruby-forum.com/t/ann-activespec-ruby-specifications-library-preview/69418/4 "2006-09-21T14:35:43Z")

</div>

Hi Michael,

I’m afraid you’ve misunderstood me when I say this was influenced by  
RSpec - this has nothing to do with BDD whatsoever. Specification is a  
well established domain pattern. This is all about making sure your  
object satisfies certain conditions _at runtime_. Your tests don’t come  
into it. Your specifications are a core part of your domain model, not  
your underlying tests (specs, in RSpecs case).

You say “should satisfy” rather than “satisfies” as the object might  
not always satisfy a particular spec, and thats ok. The best way of  
describing it is to look at it as souped up validation. However, whilst  
the should\_satisfy and satisfies\_specs? methods give you a bit of  
syntatic sugar, tthe specifications are decoupled from the objects that  
you are trying to validate. Furthermore, Specifications are _not_  
limited to the task of validating objects. They can also be used to  
find objects that meet a particular specification. There is an example  
of that here:

> **[latest posts](https://matt.berther.io/?p=612)**

There is some great information on the Specification pattern in chapter  
9 of Domain Driven Design.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 21, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/ann-activespec-ruby-specifications-library-preview/69418/5 "2006-09-21T14:41:43Z")

</div>

There is also a 19 page paper by Martin F. on the specification  
pattern. Here’s a small quote:

"The central idea of Specification is to separate the statement of how  
to match a candidate, from the candidate object that it is matched  
against. As well as its usefulness in selection, it is also valuable  
for validation and for building to order. "

> **[spec.pdf](https://www.martinfowler.com/apsupp/spec.pdf)**
>
> 76.67 KB

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 21, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/ann-activespec-ruby-specifications-library-preview/69418/6 "2006-09-21T16:00:00Z")

</div>

OK, how does this syntax work for you:

specification :user do  
requires\_presence\_of :username, :password  
requires\_confirmation\_of :password  
end

You can also do a form of specification inheritance:

specification :authenticated\_user do  
must\_satisfy :user\_specification # the above spec  
end

Incidentally, I’ve already checked in one API change…Specifications  
now use satisfied\_by? instead of pass?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 21, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/ann-activespec-ruby-specifications-library-preview/69418/7 "2006-09-21T15:41:57Z")

</div>

On Thursday 21 September 2006 14:30, removed\_email\_address@domain.invalid wrote:

> Hi Michael,
> 
> I’m afraid you’ve misunderstood me when I say this was influenced by  
> RSpec - this has nothing to do with BDD whatsoever.

I don’t think so… as I don’t remember reading that you were influenced  
by RSpec. I made up the connection to BDD entirely on my own based on  
the naming convention you’re using in ActiveSpec.

> Specification is  
> a well established domain pattern. This is all about making sure your  
> object satisfies certain conditions _at runtime_. Your tests don’t  
> come into it. Your specifications are a core part of your domain  
> model, not your underlying tests (specs, in RSpecs case).

Yes, I know, I’ve just looked it up again in DDD[\*]. I think I was being  
to dense regarding tests in my previous message. Let’s try again. It is  
okay when a test or a requirements specification (i.e. not the pattern  
incarnation) writes in terms of what some thing _should_ do. That’s  
what happens in BDD, for instance.

In the implementation of something I find it distracting and misleading  
to use “should”. Would anyone in their right mind write something like

my\_file.should\_open(…)

I don’t think so. Open the damn file or die trying, i.e. raise an  
exception. There’s just no place for “should” in here.

> You say “should satisfy” rather than “satisfies” as the object might  
> not always satisfy a particular spec, and thats ok.

Yes, it is okay if an object doesn’t satisfy a specification. Half the  
world’s population doesn’t satisfy the specification is\_female? (xor  
is\_male?). I’d feel just too hard pressed to imagine the specification  
should\_be\_female (should\_be\_male). No. Specifications express  
predicates, boolean-valued functions. Is or is not, either or.

Also, “should satisfy” is not as neutral as you make it out. In ordinary  
parlance it is not ok if something violates(!) a specification or  
requirement that it _should_ satisfy.

[\*]

> There is some great information on the Specification pattern in  
> chapter 9 of Domain Driven Design.

Incidentally, Eric Evans never uses the “should\_” prefix in his  
examples.

So, to wrap up, please eradicate the shoulds. I really fear there’s a  
nascent “should” craze coming upon us in the wake of BDD. I’m doing my  
best to stamp it out as early as I can.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 21, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/ann-activespec-ruby-specifications-library-preview/69418/8 "2006-09-21T18:42:40Z")

</div>

OK I’ve committed this API change to subversion.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 21, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/ann-activespec-ruby-specifications-library-preview/69418/9 "2006-09-21T16:26:03Z")

</div>

On Thursday 21 September 2006 15:52, removed\_email\_address@domain.invalid wrote:

> OK, how does this syntax work for you:
> 
> specification :user do  
> requires\_presence\_of :username, :password  
> requires\_confirmation\_of :password  
> end

Much better 🙂

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 22, 2006, 1:47am UTC](https://www.ruby-forum.com/t/ann-activespec-ruby-specifications-library-preview/69418/10 "2006-12-22T01:47:23Z")

</div>

I like it!! I think that it is more understandable that the validations  
that come with rails.

Keep the good work!
