Skip to content
This repository has been archived by the owner on Nov 16, 2024. It is now read-only.

Commit

Permalink
Starting to write tests
Browse files Browse the repository at this point in the history
 - Finished Categories controller
 - Finished Posts controller
 - Begin search controller
  • Loading branch information
bthuilot committed Jul 17, 2019
1 parent 26c28a5 commit a049c23
Show file tree
Hide file tree
Showing 22 changed files with 268 additions and 68 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ group :development do
end

group :test do
gem 'rails-controller-testing'
gem 'minitest'
gem 'minitest-reporters'
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ GEM
tzinfo (~> 1.1)
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
ansi (1.5.0)
arel (9.0.0)
autoprefixer-rails (9.6.0)
execjs
Expand Down Expand Up @@ -110,6 +111,11 @@ GEM
mini_mime (1.0.1)
mini_portile2 (2.4.0)
minitest (5.11.3)
minitest-reporters (1.3.6)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
msgpack (1.2.10)
multi_json (1.13.1)
nio4r (2.3.1)
Expand All @@ -134,6 +140,10 @@ GEM
bundler (>= 1.3.0)
railties (= 5.2.3)
sprockets-rails (>= 2.0.0)
rails-controller-testing (1.0.4)
actionpack (>= 5.0.1.x)
actionview (>= 5.0.1.x)
activesupport (>= 5.0.1.x)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
Expand All @@ -151,6 +161,7 @@ GEM
ffi (~> 1.0)
redcarpet (3.4.0)
regexp_parser (1.4.0)
ruby-progressbar (1.10.1)
ruby_dep (1.5.0)
rubyzip (1.2.2)
sass (3.7.4)
Expand Down Expand Up @@ -219,8 +230,11 @@ DEPENDENCIES
listen (>= 3.0.5, < 3.2)
material-sass (= 4.1.1)
material_icons
minitest
minitest-reporters
puma (~> 3.11)
rails (~> 5.2.2)
rails-controller-testing
redcarpet
sass-rails (~> 5.0)
selenium-webdriver
Expand Down
16 changes: 7 additions & 9 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def show
def update
respond_to do |format|
if @category.update(category_params)
format.html {redirect_to categories_url(@category.name, successes: ['Category was successfully updated.'])}
format.html {redirect_to categories_url(@category, successes: ['Category was successfully updated.'])}
else
format.html {render edit_categories_url(@category.name), errors: @category.errors.full_messages}
format.html {redirect_to edit_categories_url(@category, errors: @category.errors.full_messages)}
end
end
end
Expand All @@ -38,28 +38,26 @@ def create

respond_to do |format|
if @category.save
format.html {redirect_to categories_url(@category.name, successes: ['Category was successfully created.'])}
format.html {redirect_to categories_url(@category, successes: ['Category was successfully created.'])}
else
format.html {redirect_to new_categories_url(errors: @category.errors.full_messages)}
end
end
end

def edit
end

private

def set_category
@category = Category.where(name: params[:name]).first
@category = Category.find(params[:id])
end

def category_params
params.require(:category).permit(:name, :homepage)
end

def edit
end

private

def get_all_categories
@categories = Category.all
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def update
if @post.update(post_params)
format.html {redirect_to @post, notices: ['Post was successfully updated.']}
else
format.html {render :edit, error: 'Post could not be updated'}
format.html {redirect_to edit_post_url(@post, errors: @post.errors.full_messages)}
end
end
end
Expand All @@ -45,7 +45,7 @@ def destroy
category = Category.find(@post.category_id);
@post.destroy
respond_to do |format|
format.html {redirect_to categories_url(category.name), notices: ['Post was successfully destroyed.']}
format.html {redirect_to categories_url(category, notices: ['Post was successfully destroyed.'])}
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ def get_categories
end

def system_page_params
params.require(:system_page).permit(:name, :contents)
params.require(:system_page).permit(:contents)
end
end
2 changes: 1 addition & 1 deletion app/views/categories/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% provide(:title, "Edit Category") %>
<% provide(:title, "Edit #{@category.name}") %>
<div class="row justify-content-center">
<div class="col-12">
<%= form_for @category, method: :post, url: update_categories_url(@category.name) do |form| %>
Expand Down
7 changes: 4 additions & 3 deletions app/views/categories/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<% provide(:title, @category.name + " | RailsWiki") %>
<% provide(:title, @category.name) %>
<div class="row justify-content-center align-items-center">
<div class="col-9">
<h1 class="category-title"><%= @category.name %></h1>
</div>
<div class="col-3 text-center pull-right">
<a href="<%= edit_categories_url(@category.name) %>" role="button" class="btn btn-success align-middle">Edit
<a href="<%= edit_categories_url(@category) %>" role="button" class="btn btn-success align-middle">Edit
Category</a>
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#deleteCategoryModal">
Delete Category
Expand All @@ -26,7 +26,8 @@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" data-dismiss="modal">Close</button>
<a data-method="delete" href="<%= destroy_categories_url(@category.name) %>" role="button" class="btn btn-danger align-middle" data-dismiss="modal">Delete Category</a>
<a data-method="delete" href="<%= destroy_categories_url(@category) %>" role="button" class="btn btn-danger align-middle" data-dismiss="modal">Delete
Category</a>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<% for category in @categories %>
<a class="dropdown-item
<% if category.name == params[:name] %>active
<% end %>" href="<%= categories_url(category.name) %>"><%= category.name %></a>
<% end %>" href="<%= categories_url(category) %>"><%= category.name %></a>
<% end %>
<% if @categories.empty? %>
<h6 class="dropdown-header">No Categories found</h6>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title><%= yield(:title) %></title>
<title><%= yield(:title) %> | PearlWiki</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% provide(:title, "Edit Post") %>
<% provide(:title, "Edit #{@post.title}") %>
<div class="row justify-content-center">
<div class="col-12">
<%= form_for @post, method: :post, url: update_post_url(@post) do |form| %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/static_pages/edit_about.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<% provide(:title, "Edit Home") %>
<% provide(:title, "Edit About") %>
<div class="row justify-content-center">
<div class="col-12">
<%= form_for @about, method: :post, url: update_about_url do |form| %>
<div class="form-group">
<%= form.label :name, for: 'category-name-input' %>
<%= form.text_field :name, class: 'form-control disabled', id: 'category-name-input', placeholder: @about.name %>
<%= form.text_field :name, class: 'form-control disabled', id: 'category-name-input', placeholder: @about.name, disabled: true %>
</div>

<div class="form-group">
Expand Down
2 changes: 1 addition & 1 deletion app/views/static_pages/edit_home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<%= form_for @home, method: :post, url: update_home_url do |form| %>
<div class="form-group">
<%= form.label :name, for: 'category-name-input' %>
<%= form.text_field :name, class: 'form-control disabled', id: 'category-name-input', placeholder: @home.name %>
<%= form.text_field :name, class: 'form-control disabled', id: 'category-name-input', placeholder: @home.name, disabled: true %>
</div>

<div class="form-group">
Expand Down
8 changes: 4 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

# Category controller
get '/categories/new' => 'categories#new', as: 'new_categories'
get '/categories/home/:name' => 'categories#show', as: 'categories'
get '/categories/home/:id' => 'categories#show', as: 'categories'
post '/categories/create' => 'categories#create'
delete '/categories/edit/:name' => 'categories#destroy', as: 'destroy_categories'
get '/categories/edit/:name' => 'categories#edit', as: 'edit_categories'
post '/categories/edit/:name' => 'categories#update', as: 'update_categories'
delete '/categories/edit/:id' => 'categories#destroy', as: 'destroy_categories'
get '/categories/edit/:id' => 'categories#edit', as: 'edit_categories'
post '/categories/edit/:id' => 'categories#update', as: 'update_categories'

# Posts
get '/page/new' => 'posts#new', as: 'new_post'
Expand Down
8 changes: 8 additions & 0 deletions config/spring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@
tmp/restart.txt
tmp/caching-dev.txt
].each { |path| Spring.watch(path) }
Spring.after_fork do
if ENV['DEBUGGER_STORED_RUBYLIB']
ENV['DEBUGGER_STORED_RUBYLIB'].split(File::PATH_SEPARATOR).each do |path|
next unless path =~ /ruby-debug-ide/
load path + '/ruby-debug-ide/multiprocess/starter.rb'
end
end
end
19 changes: 5 additions & 14 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
#

HOME_DEFAULT_PAGE = "# Welcome to Pearl Wiki\n"\
# Create the default pages for home and about
SystemPage.create({name: "Home", contents: "# About the wiki\n"\
"Check me out on [Github](https://github.com/bthuilot/PearlWiki)!"})
SystemPage.create({name: "About", contents: "# Welcome to Pearl Wiki\n"\
"A wiki powered by rails\n"\
"\n\n"\
"To get started, create a [category](/categories/new) and [post](/page/new) or edit the [home page](/home/edit) and the [about page](/about/edit)."

ABOUT_DEFAULT_PAGE = "# About the wiki\n"\
"Check me out on [Github](https://github.com/bthuilot/RailsWiki)!"
#
SystemPage.create({name: "Home", contents: HOME_DEFAULT_PAGE})
SystemPage.create({name: "About", contents: ABOUT_DEFAULT_PAGE})
"To get started, create a [category](/categories/new) and [post](/page/new) or edit the [home page](/home/edit) and the [about page](/about/edit)."})
43 changes: 40 additions & 3 deletions test/controllers/categories_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
require 'test_helper'

class CategoriesControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end

test "should get new category page" do
get new_categories_url
assert_response :success
test_page_title "Create Category"
end

test "should get show page" do
get categories_url(categories(:software_dev))
assert_response :success
assert @controller.instance_variable_get(:@category).name == "Software Development Category", "Should display software category"
test_page_title "Software Development Category"
end

test "should get edit page" do
get edit_categories_url(categories(:admin))
assert_response :success
assert @controller.instance_variable_get(:@category).name == "Admin Category", "Admin category should be displayed"
test_page_title "Edit Admin Category"
end

test "should update categories" do
post update_categories_url(categories(:admin)), params: {category: {name: "Admin Category Changed", homepage: "This category was changed"}}
assert_redirected_to %r(^#{categories_url(categories(:admin))})
admin = @controller.instance_variable_get(:@category)
assert admin.name == "Admin Category Changed" and admin.homepage == "This category was changed"
end

test "should fail to update categories" do
post update_categories_url(categories(:admin)), params: {category: {name: "", homepage: "This category was changed"}}
assert_redirected_to %r(^#{edit_categories_url(categories(:admin))}\?)
end

test "should delete category and posts" do
assert_difference(-> {Category.count} => -1, -> {Post.count} => -10) do
delete destroy_categories_url(categories(:admin))
end
assert_redirected_to %r(^#{root_url}\?)
end

end
45 changes: 42 additions & 3 deletions test/controllers/posts_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
require 'test_helper'

class PostsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
test "should get new post page" do
get new_post_url
assert_response :success
test_page_title "New Post"
end

test "should get show page" do
get post_url(posts(:software_dev_1))
assert_response :success
post = @controller.instance_variable_get(:@post)
assert post.title == "Software Dev Post", "Should display software post"
assert post.category.name == "Software Development Category", "Should display software post category"
test_page_title "Software Dev Post"
end

test "should get edit page" do
get edit_post_url(posts(:finance_1))
assert_response :success
assert @controller.instance_variable_get(:@post).title == "Finance Post", "Finance post should be displayed"
test_page_title "Edit Finance Post"
end

test "should update posts" do
post update_post_url(posts(:finance_1)), params: {post: {title: "Finance Post Changed", contents: "This post was changed"}}
assert_redirected_to %r(^#{post_url(posts(:finance_1))})
finance_post = @controller.instance_variable_get(:@post)
assert finance_post.title == "Finance Post Changed" and finance_post.contents == "This post was changed"
end

test "should fail to update posts" do
post update_post_url(posts(:finance_1)), params: {post: {title: "", contents: "This post was changed"}}
url = @response.get_header("location")
assert_match(/errors.*Title\+can.*t\+be\+blank/, url)
assert_redirected_to %r(^#{edit_post_url(posts(:finance_1))}\?)
end

test "should delete post and posts" do
assert_difference('Post.count', -1) do
delete destroy_post_url(posts(:finance_1))
end
assert_redirected_to %r(^#{categories_url(posts(:finance_1).category)}\?)
end
end
24 changes: 21 additions & 3 deletions test/controllers/static_pages_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
def setup
# Need to load in default system pages
# These are created in `seeds.rb` so
# This command loads it in before the tests run
Rails.application.load_seed
end

test "should get home page" do
get root_url
test_page_title "Home"
all_categories = @controller.instance_variable_get(:@categories)
assert_equal all_categories.length, categories.length
end

test "should get about page" do
get about_url
test_page_title "About"
all_categories = @controller.instance_variable_get(:@categories)
assert_equal all_categories.length, categories.length
end

end
Loading

0 comments on commit a049c23

Please sign in to comment.