Skip to content
JP Barbosa edited this page Jul 23, 2015 · 2 revisions

Tests

Fix redirect after creating an article
nano test/controllers/articles_controller_test.rb
...
  test "should create article" do
    assert_difference('Article.count') do
      post :create, article: { content: @article.content, title: @article.title }
    end

    assert_redirected_to articles_path
  end
...
Run tests
rake test test
Check articles validations
nano test/controllers/articles_controller_test.rb
...
  setup do
    @article = articles(:one)
    @article_2 = articles(:two)
  end
...
  test "should not create article with too short title" do
    assert_no_difference('Article.count') do
      post :create, article: { content: @article_2.content, title: @article_2.title }
    end

    assert_template :new
  end
...
Run tests
rake test test
Add basic test for article recommendation
nano test/controllers/recommendations_controller_test.rb
require 'test_helper'
class RecommendationsControllerTest < ActionController::TestCase
  include ActiveJob::TestHelper

  setup do
    @article = articles(:one)
    @recommendation = Recommendation.new({
      article: @article,
      email: '[email protected]'
    })
    @recommendation_2 = Recommendation.new({
      article: @article,
      email: 'invalid_email_address'
    })
  end

  test "should get new" do
    get :new, article_id: @article.id
    assert_response :success
  end
end
Run tests
rake test test
Add additional tests for article recommendation
nano test/controllers/recommendations_controller_test.rb
...
  test "should not create recommendation with invalid email" do
    assert_enqueued_jobs 0 do
      post :create, article_id: @article.id, recommendation: {
        email: @recommendation_2.email,
        title: @recommendation_2.article
      }
    end

    assert_template :new
  end

  test "should create recommendation" do
    assert_enqueued_jobs 1 do
      post :create, article_id: @article.id, recommendation: {
        email: @recommendation.email,
        title: @recommendation.article
      }
    end

    assert_redirected_to articles_path
  end

...
Run tests
rake test test
Add basic tests to Git
git add .
git commit -m "Add basic tests"