Skip to content

Instantly share code, notes, and snippets.

@thibaudgg
Last active June 9, 2017 07:05
Show Gist options
  • Save thibaudgg/68d6e182d986c1c75f109dd1b2a0d6df to your computer and use it in GitHub Desktop.
Save thibaudgg/68d6e182d986c1c75f109dd1b2a0d6df to your computer and use it in GitHub Desktop.
Rails Testing - Initiation

Rails Testing - Initiation

Prerequisites

  • Clone the Cloud9 thibaudgg/rails-weblog-2 workspace. Go to https://c9.io/new/clone and then choose the MAS-RAD / Ruby on Rails team, click on the "Clone workspace" tab, then choose the thibaudgg/rails-weblog-2 workspace.
  • Ensure that PostgreSQL is still running with: sudo service postgresql start
  • Setup the TEST database with: rails db:setup RAILS_ENV=test (this will create and migrate the weblog_test database)
  • Ensure that all tests are running with rails test and rails test:system or both in one shot with rails test test.

GitHub Setup

  • Go to https://github.com/mas-rad/rails-weblog and fork the repository to your GitHub account.
  • Copy your Cloud9 SSH key from https://c9.io/account/ssh (second one) to your GitHub account: https://github.com/settings/keys.
  • From your Cloud9 workspace terminal run the following command to change the git origin to your new forked repository: git remote set-url origin git@github.com:YOUR_GITHUB_ACCOUNT/rails-weblog.git
  • Verify that the remote origin is well configured with git remote -v, you should see:
origin  git@github.com:YOUR_GITHUB_ACCOUNT/rails-weblog.git (fetch)
origin  git@github.com:YOUR_GITHUB_ACCOUNT/rails-weblog.git (push)

To do before/after EACH exercise

Run these commands before and after each exercise from your Cloud9 workspace terminal.

Before

  1. Create a new branch for the exercise with: git checkout -b exercise-NUMBER

After

  1. Ensure that all tests are passing with rails test test
  2. Review your changes with: git status
  3. Add any new files created with: git add .
  4. Commit all changes with: git commit -a -m "COMMIT MESSAGE"
  5. Push your changes to your GitHub forked repository with: git push origin
  6. From your GitHub repository create and submit a new pull-request for the exercise.

Exercise 1

Add two model tests for the User#display_name method, one with the "bob" (admin) user fixture, and another one with the "jane" (non-admin) user fixture.

Exercise 2

  1. Add a model test to ensure that a small user password (7 characters) is not valid (have a look at the existing Post model test), and check with rails test test/models/user_test.rb that the spec is failing (as the validation is not there yet).
  2. Add the minimum length (8) validation on the User#password on the User model to make the spec pass. (length validation docs)

Exercise 3

Add an authentication system test (test/system/authentication_test.b) to ensure that the user creation and login is working properly.

The test should do the following actions:

  1. Visit the home page.
  2. Click on the sign up link.
  3. Fill and submit the sign up form.
  4. Click on the login link.
  5. Fill and submit the login form.
  6. Check that the user name is present in the header.

Use the Capybara DSL for doing so.

Exercise 4

Add an admin system test (test/system/admin/posts_test.b) to ensure than an admin can edit an existing post.

The test should do the following actions:

  1. Log-in the existing "bob" fixture user (visit and fill the login form).
  2. Visit the admin post index page.
  3. Click on an post edit link.
  4. Modify the title and/or body of the post and submit the form.
  5. Check that the modifications are present in the post page.

Exercise 5

  1. Add a user_id reference column (with index and foreign key) to the "comments" table. (docs).
  2. Add the has_many :comments association to the User model and a belongs_to :author, class_name: 'User', optional: true association to the Comment model.
  3. Add two model tests for a new Comment#display_author_name method, one for a comment with an author returning the author name, another one for a comment without an author returning simply 'Guest'.

Once the tests are implemented do the following:

  1. Check with rails test test/models/comment_test.rb that the new tests are failing.
  2. Add the Comment#display_author_name method with its logic.
  3. Check with rails test test/models/comment_test.rb that the tests are passing.

Exercise 6

Add two comment system tests ensuring that a logged-in user and a guest can both create a comment.

The tests should do the following actions:

  1. Visit the home page.
  2. Log-in the user (or not if a guest).
  3. Click on a post link.
  4. Fill and submit the comment form
  5. Check that the comment and its author name are present.

Once the tests are implemented do the following:

  1. Check with rails test test/models/comment_test.rb that the tests are failing.
  2. Modify the CommentsController#create action to merge the current_user.id as the user_id params. See that Stack Overflow thread for more details.
  3. Check with rails test test/models/comment_test.rb that the tests are passing.

Use the Capybara DSL for doing so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment