RSpec on Rails Matchers plugin
Written by matt on January 3rd, 2008
RSpec is an awesome testing framework. On top of being the first Ruby BDD framework the core team is doing a great job in enhancing our testing experience and therefore the quality of our code.
This time, I don't want to introduce to the latest changes but instead showing you what Josh Knowles, Bryan Helmkamp and myself came up with.
RSpec on Rails matchers plugin + TextMate Bundle
Matchers are some sort of helpers that will help you cleaning up your tests. We simply came up with a collection of matchers that we think will make your like easier.
We divided the matchers in 3 categories:
Associations
Verify that the association has been defined. (doesn't verify that the association works!)
Usage examples:
1 2 3 4 5 6 7 8 |
@post.should have_many(:comments)
@comment.should belong_to(:post)
@user.should have_one(:social_security_number)
@project.should have_and_belong_to_many(:categories)
|
Validations
Verify that a validation has been defined. (doesn't test the validation itself)
1 2 3 4 5 6 7 8 9 10 |
object.should validate_presence_of(:attribute)
object.should validate_confirmation_of(:attribute)
object.should validate_uniqueness_of(:attribute)
object.should validate_length_of(:attribute, :between => 5..10)
object.should validate_length_of(:attribute, :is => 5)
|
Views
My personal favorite matchers, you can now do stuff like:
1 2 3 4 5 6 7 8 9 10 11 12 |
it "should render new form" do
render "/users/new.html.erb"
response.should have_form_posting_to(users_path) do
with_text_field_for(:user_name)
with_text_area_for(:user_address)
with_text_field_for(:user_login)
with_text_field_for(:user_email)
with_submit_button
end
end
|
Check the readme for more information and details on the added matchers. I personally recommend you try the TextMate Bundle on top of being a perfect tool for lazy devs, it also lists all the available matchers and is an excellent way of learning.
We just released our first release yesterday, this is not a final version and we will keep on improving the code. If you have suggestions and patches feel free to open a ticket there.
Comments
-
Thanks, these are great and I've added a patch for an edit form matcher also.
I wondered if these are really appropriate for packaging as a plugin with an init.rb, because won't they then get loaded when the app is run? I've just piston imported them myself (SVN external will also work) into railsroot/spec/matchers and then required them in my spechelper.rb
-
Hey Matt, These look really nice. I haven't been able to get them working as advertised on the view specs however. I posted my woes to the rpsec-users list (here is a copy if your not on that list: http://pastie.caboo.se/139692). Anyways, if you could maybe look at what I'm doing I would appreciate it because I would really like to start using these. Thanks, Ben
-
Hi Matt,
I've added tests like this to the Shoulda project, which is like rSpec, but based on Test::Unit. The test aren't perfect, but have had a few eyes on them, and have been used in a lot of projects. I encourage you to check them out, and scavenge what you will for rSpec on Rails.
https://svn.thoughtbot.com/plugins/shoulda/trunk
Cheers, Tammer
-
What does it mean that you're verifying a validation has been defined, but not testing the validation itself? Looking at the code, it seems like the matcher does something that would make the validation fail and then ensures there's an error on the attribute. It sounds like testing the validation to me, but you could be meaning something else.
By the way, having only one test doesn't really check the validation very well. If you're only going to have one test, it's true that it's better to check that there's an error when the attribute is nil, but that's only half the story. The other half is making sure there's no error if the attribute is not nil. Too bad you can't reliably check for anything more than a message when you're trying to pin down validations.
Too bad validations couldn't be better.


