Carlos Brando

Nome do Jogo

Remarkable now supports all ActiveRecord validations

Good times! It has been just one week since I announced Diego Carrion and José Valim as members of the Remarkable core team and we already put some effort into making a new shiny release! We are going to cover what’s new on this Remarkable version, step by step.

1. All ActiveRecord validations are supported with all options

In other words, you can write your spec validation macros the same way as you do in ActiveRecord, without consulting the documentation, without worrying if any validation is supported or not, without checking which options are allowed or not. Check it out:

describe User do
  it { should validate_uniqueness_of(:email) }
  it { should validate_presence_of(:email, :password) }
  it { should validate_confirmation_of( :email, :password) }

  it { should validate_acceptance_of(:terms) }
  it { should validate_inclusion_of(:gender, %w(m f), :message => 'whoa! what are you then?') }
  it { should validate_length_of(:password, :within => 6..20) }
  it { should validate_numericality_of(:age, :greater_than_or_equal_to => 18, :allow_nil => false) }

  it { should allow_mass_assignment_of(:password, :email) }
  it { should not_allow_mass_assignment_of(:salt, :hashed_password) }

Or if you prefer:

describe User do
  should_validate_uniqueness_of :email
  should_validate_presence_of :email, :password
  should_validate_confirmation_of :email, :password

  should_validate_acceptance_of :terms
  should_validate_inclusion_of :gender, %w(m f), :message => 'whoa! what are you then?'
  should_validate_length_of :password, :within => 6..20
  should_validate_numericality_of :age, :greater_than_or_equal_to => 18, :allow_nil => false

  should_allow_mass_assignment_of :password, :email
  should_not_allow_mass_assignment_of :salt, :hashed_password

We updated the wiki and it’s already reflecting the new macros. But you might ask, what happened with the older ones? The older ones are going to be deprecated and you will see a message while running the tests.

What you might have noticed above is that should_protect_attributes now became should_not_allow_mass_assignment_of. This one was based on Shoulda last changes, so who are porting tests from Shoulda gets everything compatible.

2. Pending macros

We are not just bringing macros to Rspec, we are bringng Rspec to macros! So let’s suppose you are doing some code refactoring and you know some tests will fail while you clean the house, what do you do then?

In Rspec, you can do:

xit { should validate_numericality_of(:age) }

Well, now you can do just the same with macros:

xshould_validate_numericality_of :age

And it’s even better because it shows you a nice message:

Example disabled: should validate numericality of age


3. Full I18n support

You know how validations macros (as validate_presence_of) work? Let’s talk about it a little bit.

When you say that we should validate presence of e-mail, we set the e-mail to nil and save the object. Then we search in all error messages in e-mail, if it has a “can’t be blank” message, your validation is working properly and then the test passes, otherwise, the test fails. The problem is, if you change the error message in your application and do not change on the macro, we will never be able to find the message “can’t be blank” and then the tests will always pass.

This is valid for almost rspec matchers, not only on Remarkable. But what’s new is, if you change the message using the I18n YAML files, we will find the message properly and you don’t have to pass :message in your macros. This is new and we are currently the only one doing this.

But remember, if ever you set the message directly in your models like this:

validates_inclusion_of :gender, %w(m f), :message => 'whoa! what are you then?'

Don’t forget to do the same in your macros:

should_validate_inclusion_of :gender, %w(m f), :message => 'whoa! what are you then?'



Remarkable is available on GitHub and to install it, just do on your console:

gem sources -a
sudo gem install "carlosbrando-remarkable"

And then on your RAILS_ROOT/config/environment.rb:

config.gem "carlosbrando-remarkable", :lib => "remarkable", :source => ""

Any doubts? Any bug?

We want to hear you! So you can use the Remarkable google group, our bug tracking system, Github comments, whatever suits you better!

See you there!