stalno u pokretu

< IT & tech


01. 09. 2014. api * back end * bdd / tdd * cucumber * ruby * ruby gem

Developing rails REST API with Cucumber

In developing a rails API, BDD way with cucumber, I opted for several compromises.

I tried to hide as much of incidental details as possible, in order to keep the definitions readable to the stakeholders, but still retained some imperativeness, so that it can serve as documentation for the client developers.

json_spec gem

To help me with JSON management, I am using the json_spec gem. It has a number of useful matchers (such as be_json_eql, include_json, have_json_size…), helpers (parse_json, normalize_json…) and some common cucumber steps for checking the responses.

Installation and configuration is straightforward, it needs to be added to the Gemfile:

and to make use of json_spec cucumber steps they need to be required, and json_spec also needs to know the last_json. Rack::Test is part of rails, so its last_response is what I use.

Writing the definitions

The pragmatic programmers’ The cucumber book in a chapter about testing the REST web services, proposes that the definitions should be written like this :

At first I tried doing it like that, but soon my feature files became quite hard to read, especially if a scenario needed to include several tables and a JSON. So I decided to get rid of as much of incidental details as possible, but I kept the imperative definitions that call specific endpoints with appropriate methods, because they serve as a good reference to the client developers and they don’t bother the stakeholders much.

After applying these principles, the previous example would look something like this:

Instead of seeding the data from the definitions, I rather just specify what I want in the definition and do the seeding in the steps. I check if the response is what was expected, I add some additional fields (success, status, href) and check for those, I return the fruits in an array, and check the array length and contents:

Of course, transformations, helper methods and constants should go into their own respective files.

Checking the http headers

Here are several handy steps for checking the ACCEPT, CONTENT_TYPE and AUTHENTICATION headers:

Testing authentication

Rather then calling that authentication step from the definitions, its much nicer to nest it in a definition like this:

When I authenticate as admin

and this would be the step:

Resources


Related posts


03. 09. 2014. api * back end * ruby * ruby gem * security
Devise as authentication solution for rails API

Authentication token, the use of sessions, intsallation and setup, configuring the model, the routes, application controller, custom devise controllers, testing the controllers
[ reading time: ~ 9 min. ]

05. 08. 2014. cucumber * ruby gem * bdd / tdd
Cucumber gem

notes
[ reading time: ~ 1 min. ]

15. 08. 2014. api * back end * ruby
API - design, implementation, testing

notes and resources
[ reading time: ~ 3 min. ]

07. 09. 2014. api * back end * ruby
Building a JSON API in rails

notes
[ reading time: ~ 1 min. ]

05. 08. 2014. bdd / tdd * ruby gem
Factorygirl

notes
[ reading time: ~ 1 min. ]

05. 08. 2014. bdd / tdd * rspec * ruby gem
Rspec gem

notes
[ reading time: ~ 3 min. ]

10. 08. 2014. api * back end
Representational state transfer (REST)

notes
[ reading time: ~ 3 min. ]

18. 08. 2014. ruby gem * bdd / tdd
Forgery gem

notes
[ reading time: ~ 1 min. ]

30. 08. 2014. ruby gem * bdd / tdd
Faker

notes
[ reading time: ~ 1 min. ]

04. 09. 2014. api * back end
Design principles of the REST-ful JSON API (HATEOAS)

REST, JSON, API, HATEOAS, media types, top level resource representations, url templates, compound documents, urls, responses, errors, JSend response types, HTTP Status codes
[ reading time: ~ 12 min. ]

12. 09. 2014. back end * ruby
ActiveRecord notes

Uniqueness on a combination of fields, inclusion in array, getting all model associations, overriding model getters and setters, serialization...
[ reading time: ~ 2 min. ]

17. 09. 2014. back end * i18n&l10n * ruby
i18n gem advanced features – Ruby on Rails internationalization

Using Different Backends for Ruby i18n gem, chaining backends, caching is an i18n gem advanced feature, fallbacks, Translation Metadata, cascading lookups, translation symlinks, using custom exception handlers
[ reading time: ~ 14 min. ]

17. 09. 2014. back end * i18n&l10n * padrino * ruby * sinatra
Internationalization for Ruby – i18n gem

i18n for plain Ruby & the i18n gem, i18n for Ruby on Rails, i18n for Sinatra, i18n for Padrino
[ reading time: ~ 20 min. ]

17. 09. 2014. back end * i18n&l10n * ruby
Internationalization for Ruby with the r18n gem

Installation and setup, translation lookup, filters for translations processing, pluralization, translating activeRecord models and plain Ruby objects, locale settings, r18n wrappers for rails and sinatra, r18n vs i18n
[ reading time: ~ 9 min. ]

17. 09. 2014. back end * gettext * i18n&l10n * ruby
Ruby gettext internationalization tutorial on using the fast_gettext gem

Setup, translation lookup, managing translations (MO/PO, db, yaml), rails plugin, pluralization, defaults, multiple repositories, logging...
[ reading time: ~ 9 min. ]

17. 11. 2014. ruby gem * bdd / tdd
Capybara

notes
[ reading time: ~ 2 min. ]

22. 11. 2014. back end * deployment * ruby
rails deployment

Setting up Debian 7 server on VPS, nginx, php-fpm, ruby, unicorn/phusion passanger, essentials, ssh keys, capistrano...
[ reading time: ~ 2 min. ]

10. 12. 2014. back end * ruby gem
Capistrano

notes
[ reading time: ~ 1 min. ]

07. 08. 2014. ruby gem
Devise gem

notes
[ reading time: ~ 1 min. ]

17. 08. 2014. api * featured * security
Authentication techniques for REST API

API keys, username & password, authentication token, security concerns, nonce
[ reading time: ~ 5 min. ]

21. 08. 2014. database * ruby
Adding UUID to rails

notes
[ reading time: ~ 1 min. ]

17. 09. 2014. back end * cakephp * codeigniter * i18n&l10n * php * symfony * yii * zend
Internationalization How To for the 5 most popular PHP frameworks

In this how-To you will read about using CodeIgniter, CakePHP, Zend, Yii and Symfony
[ reading time: ~ 8 min. ]

17. 09. 2014. back end * fuelphp * i18n&l10n * laravel * php
PHP internationalization frameworks: Laravel and FuelPHP

The article covers two more PHP frameworks that are frequently used, Laravel and FuelPHP
[ reading time: ~ 6 min. ]

17. 09. 2014. back end * i18n&l10n * php
PHP internationalization – i18n mechanisms tutorial

Static web and internationalization, dynamic web applications, localizing strings directly in the code, storing the strings in a relational database, message catalogues (string arrays), JSON, use of resource files
[ reading time: ~ 10 min. ]

17. 09. 2014. back end * gettext * i18n&l10n * php
PHP internationalization with gettext tutorial

Installation, portable object template files, plurals, an example of a PO file, directory structure, machine object files, gettext caching problems, setting up PHP for internationalization with gettext...
[ reading time: ~ 18 min. ]

17. 09. 2014. back end * i18n&l10n
Resource file formats

ini files, properties files, iOS .strings files, YAML files, RESX and RESW files, RESJSON files
[ reading time: ~ 4 min. ]

24. 11. 2014. back end * dev_tool
nginx notes

notes
[ reading time: ~ 1 min. ]

Ovaj sajt ne sadrži first party kukije i druge mehanizme za aktivno praćenje poseta/ponašanja (facebook, google analytics, itd...). Na nekim stranicama se nalaze embedovani youtube video klipovi i google mape koji učitavaju svoje kukije.