Testing,Documentation and Others
Past two weeks, i have studied some useful gems in rails. Here are the summary and tips.
Cucumber
Cucumber is the Behavior Driven Development(BDD). Writing testing is the most important in software development. Without testing case it’s hard to refactor any code in the future.
For instance: my site provide REST API with JSON and require devise login to use.
How to test?
Add to Gemfile. Suggest to use rspec and factory_gril gems together.
1 2 3 4 5 6 7 8 9 |
|
Run rails g cucumber:install
to initial features/ structure.
Modify config/initializers/devise.rb if you use devise and need to test sign_out function.
1
|
|
Create api.feature in features/. You should describe your feature by plain english.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Create api_steps.rb in features/step_definitions/ and users.rb in spec/factories/.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
1 2 3 4 5 6 7 |
|
Here are some items should be take care.
1. Use capybara to visit sign_in page and click it to sign in
2. With | name | yyy |
, you can pass a table into steps function. I use it to create Category.
3. Use page.driver.get api_category_path
to send GET request with authentication session(Because you have sign in)
Run cucumber to test and get report.
Ransack
Search your data in active record. I add the search function into rails101_groupme project.
The usage is quite simple. The groupme site has a search field in the navigation bar to search all posts.
Add a method in application_controller.rb because we have a search field in nav bar and every controller need to render it.
1 2 3 4 5 6 7 8 |
|
Add controller to handle search request.
1 2 3 4 5 |
|
Add search path in route. Only accepts post here.
1 2 3 |
|
Add search view. Ransack has search_form_for
tag and it’s nice!
1 2 3 |
|
:content_or_title_cont
means we want to search content or title that matches search keyword.
If you want to search title column only, you should use :title_cont
. _cont
is necessary and means column’s content.
column name is the same with the active record you defined.
Yard
Documentation is important. The Yard syntax is like Java and i like it.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Yard supports markdown syntax in describe your method. _id_
is the italic word in markdown.