Giter Club home page Giter Club logo

rails-api-bdd-iteration's Introduction

General Assembly Logo

Iterative Development of Rails APIs

Prerequisites

Objectives

By the end of this lesson, students should be able to:

  • Explain the value of iterative development.
  • Write concise, associated routes.
  • Write model unit specs for associations.
  • Write model unit specs for validations.

Preparation

  1. Fork and clone this repository.
  2. Install dependencies with bundle install.
  3. Run rake db:create db:migrate
  4. Create config/secrets.yml and run rake secret twice to generate tokens.
test:
  secret_key_base: XXX
  secret_token: XXX

Demo: Authenticated Requests for Articles

Let's check out how our requests/articles_spec.rb has changed since our last pass at this.

Demo: Associated Request Specs for Comments of Articles

Let's look into requests/article_comments_spec.rb and examine what is required to make a request on associations. What do you notice about our endpoints?

Code-along: Associate Comments with Articles

Start with modifying our Comments migration (rails g migration AddArticleToComments).

Then, we will update our Comments and Articles models to handle this new relationship.

Discussion: Choosing Endpoints for Article Comments

Check out our nested routes in routes.db.

Before we associated Comments to Articles, our Articles routes looked like this:

resources :articles, except: [:new, :edit]

Now that we're associating Comments to Articles, we can take advantage of Rails' nested routes feature. Hence, our routes for these resources now look like this:

resources :articles, except: [:new, :edit] do
  resources :comments, only: [:index, :create]
end
resources :comments, except: [:new, :edit]

Comments' :index and :create methods are collection routes in Rails, meaning their actions act upon the collection of their resource, hence why they are nested (i.e., showing ALL comments associated to an article).

Comments' :update, :show, and :destroy methods are member routes in Rails, meaning their actions act upon a single member of the resource collection (i.e., deleting ONE comment).

Let's run rake routes and take a look at what this gives us.

Discussion: Shallow Routes

Nested routes offer many advantages, but can still look a little sloppy depending on the restrictions you need to apply to each resource, respectively.

Rails to the rescue!

We can leverage shallow routes to generate the same routes.

resources :articles, except: [:new, :edit] do
  resources :comments, except: [:new, :edit], shallow: true
end

Here, adding shallow: true to our comments resources generates all collection routes for the child route association (i.e., :index and :create) as well as all other member routes that are not nested (i.e., :show, :update, :destroy).

It's your choice whether you use nested or shallow routes. Testing for Article/Comment associated routes can be found at routing/article_comments_spec.rb.

Code-along: Test Article Model

In spec/models/article_spec.rb, let's test to see if we:

  1. are associating comments to articles
  2. have set our inverse_of record
  3. are deleting comments associated to articles when articles are deleted

Lab: Test Comments Model

In spec/models/comment_spec.rb, use the tests we created for the Article model to guide your tests to ensure you:

  1. are associating articles to comments
  2. have set your inverse_of record

Code-along: Iterate over Article Model to Ensure Validations

Using our BDD skills, let's create tests to check that our Article model is validating the presence of content and title. We don't want articles created that omit either.

We will create our tests first and let those drive us towards an adequately-validated model.

Lab: Iterate over Comment Model to Ensure Validations

Your turn. Let your test(s) drive you towards validating the presence of a new comment's content.

Bonus: Write a Tested, Behavior-Driven Blog API in Rails

Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.

rails-api-bdd-iteration's People

Contributors

laurenfazah avatar fishermanswharff avatar tibbon avatar jrhorn424 avatar

Watchers

Shireen Kheradpey avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.