semantic_rails_view_helpers/README.md

98 lines
2.4 KiB
Markdown
Raw Permalink Normal View History

2013-01-24 23:08:21 +00:00
Make your Rails Capybara testing even faster and more accurate! Looking for hardcoded text strings is for the birds.
A lot of this assumes you're using a form builder to generate forms, like Formtastic. It's faster that way.
2012-11-19 13:39:43 +00:00
2012-12-08 15:00:29 +00:00
## Your Views
2012-11-19 13:39:43 +00:00
2012-12-08 16:28:07 +00:00
It's easier to find things in Capybara tests if you add extra `data` attributes to fields and such.
This gem does that for you if you link to and refer to things in a certain way. If you do, your
tests turn from text blob and CSS selector messes to nice, clean, simple references to objects
and attributes.
2013-01-24 23:08:21 +00:00
### Linking to things
Link to things using `link_to_model`, `link_to_model_action`, `link_to_collection`, and `link_to_route`.
Data attributes will be added that Capybara can then find later, and quickly:
``` haml
#menu
= link_to_route :root
= link_to_collection [ :admin, :users ]
= link_to_model current_user
= link_to_model_action current_user, :edit
```
``` ruby
# finding those things
within '#menu' do
find_semantic_link(:root)
find_semantic_link(:users)
find_object(@user)
find_action(:edit)
end
```
### Form fields
Don't worry about tying your field entry with text labels. It's much easier to look for attributes by name:
``` ruby
find_input(:first_name).set("first name")
find_input(:last_name).set("last name")
set_input(:gender, 'male')
find_submit.click
```
2012-12-08 16:28:07 +00:00
### Attributes
2012-12-08 15:00:29 +00:00
Write out attributes using `attributes_for`:
2012-11-19 13:39:43 +00:00
2012-12-08 15:00:29 +00:00
``` haml
#object
= attributes_for object do |f|
%h2= f.field(:name)
%h3= f.field(:description)
```
2012-11-19 13:39:43 +00:00
2012-12-08 15:00:29 +00:00
Then look for those fields using Capybara! Because you shouldn't care about the DOM, just that your
field is in there:
2012-11-19 13:39:43 +00:00
2012-12-08 15:00:29 +00:00
``` ruby
@object = Object.create!(:name => @name, :description => @description)
2012-11-19 13:39:43 +00:00
2012-12-08 15:00:29 +00:00
visit object_path(@object)
2012-11-19 13:39:43 +00:00
2012-12-08 15:00:29 +00:00
find_attribute(:name, @object.name)
find_attribute(:description, @object.description)
```
2012-12-08 16:28:07 +00:00
You can even make simple tables, a la Active Admin:
``` haml
#object
= attributes_table_for object do |f|
= f.field :name
= f.field :description
```
2013-02-25 15:32:59 +00:00
### Active Admin
You can add semantic data to Active Admin's `attributes_table`s in `show` views. Just `require 'semantic_rails_view_helpers/active_admin'`
in an initializer and you can then target attributes in `show` views.
2012-12-08 16:28:07 +00:00
## Not Finding Things
Sometimes the absence of a thing is just as important as the presence of a thing. Make it easy on yourself:
``` ruby
# selector's not there
dont_find('#user')
2013-02-25 18:50:25 +00:00
# ...after you've already found something...
find('.node').dont_find('.something_inside')
2012-12-08 16:28:07 +00:00
```
2013-01-24 23:08:21 +00:00