Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: fonsecajavier/my_blog
base: 2890b49c73
...
head fork: fonsecajavier/my_blog
compare: e57afac8cd
  • 2 commits
  • 34 files changed
  • 0 commit comments
  • 1 contributor
Commits on Nov 26, 2012
Javier Fonseca V Adds some missing validations to Comment model. Fixes some redirect/f…
…lashes issues. All thanks to tests
ffb1862
Javier Fonseca V Removes all old unit tests skeletons. Adds rspec_rails + shoulda + fa…
…ctory_girl_rails + simple_cov. Adds tests for current 100% coverage
e57afac
Showing with 312 additions and 125 deletions.
  1. +2 −1  .gitignore
  2. +1 −0  .rspec
  3. +11 −0 Gemfile
  4. +31 −0 Gemfile.lock
  5. +1 −1  app/controllers/comments_controller.rb
  6. +2 −2 app/controllers/posts_controller.rb
  7. +2 −0  app/models/comment.rb
  8. +5 −1 config/database.yml.example
  9. +29 −0 spec/controllers/comments_controller_spec.rb
  10. +112 −0 spec/controllers/posts_controller_spec.rb
  11. +27 −0 spec/factories.rb
  12. +8 −0 spec/models/comment_spec.rb
  13. +9 −0 spec/models/post_spec.rb
  14. +9 −0 spec/models/user_spec.rb
  15. +7 −0 spec/routing/routes_spec.rb
  16. +44 −0 spec/spec_helper.rb
  17. +9 −0 spec/support/controller_macros.rb
  18. +3 −0  spec/support/devise.rb
  19. 0  test/fixtures/.gitkeep
  20. +0 −11 test/fixtures/comments.yml
  21. +0 −11 test/fixtures/posts.yml
  22. +0 −11 test/fixtures/users.yml
  23. 0  test/functional/.gitkeep
  24. +0 −9 test/functional/comments_controller_test.rb
  25. +0 −24 test/functional/posts_controller_test.rb
  26. 0  test/integration/.gitkeep
  27. +0 −12 test/performance/browsing_test.rb
  28. +0 −13 test/test_helper.rb
  29. 0  test/unit/.gitkeep
  30. +0 −7 test/unit/comment_test.rb
  31. +0 −4 test/unit/helpers/comments_helper_test.rb
  32. +0 −4 test/unit/helpers/posts_helper_test.rb
  33. +0 −7 test/unit/post_test.rb
  34. +0 −7 test/unit/user_test.rb
View
3  .gitignore
@@ -14,4 +14,5 @@
/log/*.log
/tmp
database.yml
-.DS_Store
+.DS_Store
+coverage
View
1  .rspec
@@ -0,0 +1 @@
+--color
View
11 Gemfile
@@ -26,6 +26,17 @@ gem 'jquery-rails'
gem 'pg', '0.14.1'
gem 'gon', '4.0.1'
+group :test, :development do
+ gem "rspec-rails", "~> 2.0"
+end
+
+group :test do
+ gem 'simplecov', :require => false
+ gem 'shoulda'
+ gem 'factory_girl_rails'
+ # gem 'database_cleaner' # might need it in the future (https://github.com/bmabey/database_cleaner)
+end
+
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
View
31 Gemfile.lock
@@ -43,9 +43,15 @@ GEM
orm_adapter (~> 0.1)
railties (~> 3.1)
warden (~> 1.2.1)
+ diff-lcs (1.1.3)
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
+ factory_girl (4.1.0)
+ activesupport (>= 3.0.0)
+ factory_girl_rails (4.1.0)
+ factory_girl (~> 4.1.0)
+ railties (>= 3.0.0)
gon (4.0.1)
actionpack (>= 2.3.0)
json
@@ -90,14 +96,35 @@ GEM
rake (10.0.2)
rdoc (3.12)
json (~> 1.4)
+ rspec-core (2.12.0)
+ rspec-expectations (2.12.0)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.12.0)
+ rspec-rails (2.12.0)
+ actionpack (>= 3.0)
+ activesupport (>= 3.0)
+ railties (>= 3.0)
+ rspec-core (~> 2.12.0)
+ rspec-expectations (~> 2.12.0)
+ rspec-mocks (~> 2.12.0)
sass (3.2.3)
sass-rails (3.2.5)
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
+ shoulda (3.3.2)
+ shoulda-context (~> 1.0.1)
+ shoulda-matchers (~> 1.4.1)
+ shoulda-context (1.0.1)
+ shoulda-matchers (1.4.1)
+ activesupport (>= 3.0.0)
simple_form (2.0.4)
actionpack (~> 3.0)
activemodel (~> 3.0)
+ simplecov (0.7.1)
+ multi_json (~> 1.0)
+ simplecov-html (~> 0.7.1)
+ simplecov-html (0.7.1)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
@@ -121,11 +148,15 @@ PLATFORMS
DEPENDENCIES
coffee-rails (~> 3.2.1)
devise (= 2.1.2)
+ factory_girl_rails
gon (= 4.0.1)
jquery-rails
pg (= 0.14.1)
rails (= 3.2.2)
+ rspec-rails (~> 2.0)
sass-rails (~> 3.2.3)
+ shoulda
simple_form (= 2.0.4)
+ simplecov
sqlite3
uglifier (>= 1.0.3)
View
2  app/controllers/comments_controller.rb
@@ -6,7 +6,7 @@ def create
if @comment.save
redirect_to post_path(@post), :notice => "Your comment was submitted. Thank you"
else
- redirect_to post_path(@post), :error => "Your comment couldn't be submitted"
+ redirect_to post_path(@post), :alert => "Your comment couldn't be submitted"
end
end
end
View
4 app/controllers/posts_controller.rb
@@ -21,7 +21,7 @@ def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
- redirect_to posts_path, :notice => "Your post was edited succesfully"
+ redirect_to post_path(@post), :notice => "Your post was edited succesfully"
else
flash[:error] = "There were some errors editing your post"
render action: "edit"
@@ -38,7 +38,7 @@ def create
@post.user = current_user
if @post.save
- redirect_to posts_path, :notice => "Your post was created succesfully"
+ redirect_to post_path(@post), :notice => "Your post was created succesfully"
else
flash[:error] = "There were some errors creating your post"
render action: "new"
View
2  app/models/comment.rb
@@ -1,3 +1,5 @@
class Comment < ActiveRecord::Base
belongs_to :post
+
+ validates_presence_of :nickname, :message, :post_id
end
View
6 config/database.yml.example
@@ -14,4 +14,8 @@ staging:
production:
<<: *defaults
- database: my_blog_production
+ database: my_blog_production
+
+test:
+ <<: *defaults
+ database: my_blog_test
View
29 spec/controllers/comments_controller_spec.rb
@@ -0,0 +1,29 @@
+require "spec_helper"
+
+describe CommentsController do
+ before(:all) do
+ @existing_post = create(:post)
+ end
+
+ it "should allow to enter a new comment to a given post and store it if valid. Should show the post page with a flash notice message" do
+ expect {
+ post :create, {
+ post_id: @existing_post.id,
+ comment: attributes_for(:comment)
+ }
+ }.to change { @existing_post.comments.count }.by(1)
+ flash[:notice].should_not be_nil
+ response.should redirect_to(post_path(@existing_post))
+ end
+
+ it "shouldn't allow to enter an invalid comment to a given post. Should show the post page with a flash error message" do
+ expect {
+ post :create, {
+ post_id: @existing_post.id,
+ comment: attributes_for(:invalid_comment)
+ }
+ }.to change { @existing_post.comments.count }.by(0)
+ flash[:alert].should_not be_nil
+ response.should redirect_to(post_path(@existing_post))
+ end
+end
View
112 spec/controllers/posts_controller_spec.rb
@@ -0,0 +1,112 @@
+require "spec_helper"
+
+describe PostsController do
+
+ context "all users" do
+ it "should be presented with a list of all posts at index, ordered from newest to oldest" do
+ 4.times { create(:post) }
+ get :index
+ assigns[:posts].should have_at_least(4).posts
+ assigns[:posts].map(&:id).should eql(
+ assigns[:posts].sort { |x, y| y[:id] <=> x[:id] }.map(&:id)
+ )
+ response.should render_template(:index)
+ end
+
+ it "should be presented with a requested post and its comments in the show action" do
+ existing_post = create(:post)
+ 4.times { create(:comment, post_id: existing_post.id) }
+ get :show, id: existing_post.id
+ assigns[:post].should == existing_post
+ assigns[:comments].count.should eql(4)
+ response.should render_template(:show)
+ end
+
+ end
+
+ context "not logged in user should be redirected to sign in path when" do
+ it "trying to go to the new post page" do
+ get :new
+ response.should_not be_success
+ end
+
+ it "submitting form on new post page" do
+ post :create
+ response.should_not be_success
+ end
+
+ it "trying to go to the edit post page" do
+ get :edit, id: create(:post).id
+ response.should_not be_success
+ end
+
+ it "trying to submit an update for an existing post. The post shouldn't be changed" do
+ existing_post = create(:post)
+ put :update, {
+ id: existing_post.id,
+ post: { title: "Title changed" }
+ }
+ existing_post.reload.title.should_not eql("Title changed")
+ response.should_not be_success
+ end
+ end
+
+ context "logged in user" do
+ login_user
+
+ it "should be able to see the new post page" do
+ get :new
+ response.should render_template(:new)
+ end
+
+ it "should be able to create a new post and be redirected to such post page with a flash notice message" do
+ expect {
+ post :create, {:post => attributes_for(:post)}
+ created_post = assigns[:post]
+ created_post.should_not be_new_record
+ response.should redirect_to(post_path(created_post))
+ created_post.user.should == @logged_in_user
+ flash[:notice].should_not be_nil
+ }.to change { Post.count }.by(1)
+ end
+
+ it "shouldn't be able to create an invalid post. Should be instead redirected to the new post page with a flash error message" do
+ expect {
+ post :create, {:post => attributes_for(:invalid_post)}
+ created_post = assigns[:post]
+ created_post.should be_new_record
+ response.should render_template(:new)
+ flash[:error].should_not be_nil
+ }.to change { Post.count }.by(0)
+ end
+
+ it "should be able to go the 'edit a post' page" do
+ existing_post = create(:post)
+ get :edit, id: existing_post.id
+ response.should render_template(:edit)
+ end
+
+ it "should be able to submit an update for an existing post. The post should be changed and the user redirected to the show page with a flash notice message" do
+ existing_post = create(:post)
+ put :update, {
+ id: existing_post.id,
+ post: { title: "Title changed" }
+ }
+ existing_post.reload.title.should eql("Title changed")
+ flash[:notice].should_not be_nil
+ response.should redirect_to(post_path(existing_post))
+ end
+
+ it "shouldn't be able to update an existing post with invalid values. Should be presented with the same edit page and a flash error message" do
+ existing_post = create(:post)
+ put :update, {
+ id: existing_post.id,
+ post: { title: "" }
+ }
+ existing_post.reload.title.should_not eql("")
+ flash[:error].should_not be_nil
+ response.should render_template(:edit)
+ end
+ end
+
+end
View
27 spec/factories.rb
@@ -0,0 +1,27 @@
+FactoryGirl.define do
+ factory :user do
+ sequence(:full_name) {|n| "Fulano De Tal N#{n}"}
+ sequence(:email) {|n| "fulano_#{n}@detal.com"}
+ password "password"
+ end
+
+ factory :post do
+ title "My imaginative title"
+ content "Don't know what to put in here"
+ user
+
+ factory :invalid_post do
+ title ""
+ end
+ end
+
+ factory :comment do
+ post
+ nickname "whoever you want"
+ message "hey nice thoughts!"
+
+ factory :invalid_comment do
+ nickname ""
+ end
+ end
+end
View
8 spec/models/comment_spec.rb
@@ -0,0 +1,8 @@
+require "spec_helper"
+
+describe Comment do
+ it { should belong_to(:post) }
+ it { should validate_presence_of(:nickname) }
+ it { should validate_presence_of(:message) }
+ it { should validate_presence_of(:post_id) }
+end
View
9 spec/models/post_spec.rb
@@ -0,0 +1,9 @@
+require "spec_helper"
+
+describe Post do
+ it { should belong_to(:user) }
+ it { should have_many(:comments) }
+ it { should validate_presence_of(:title) }
+ it { should validate_presence_of(:content) }
+ it { should validate_presence_of(:user_id) }
+end
View
9 spec/models/user_spec.rb
@@ -0,0 +1,9 @@
+require 'spec_helper'
+
+describe User do
+ subject { create(:user) }
+ it { should validate_presence_of(:full_name) }
+ it { should validate_presence_of(:email) }
+ it { should validate_uniqueness_of(:email) }
+ it { should have_many(:posts).dependent(:destroy) }
+end
View
7 spec/routing/routes_spec.rb
@@ -0,0 +1,7 @@
+require "spec_helper"
+
+describe "Routes Helpers" do
+ it "routes root (/) to posts#index" do
+ get("/").should route_to("posts#index")
+ end
+end
View
44 spec/spec_helper.rb
@@ -0,0 +1,44 @@
+require 'simplecov'
+SimpleCov.start 'rails'
+
+# This file is copied to spec/ when you run 'rails generate rspec:install'
+ENV["RAILS_ENV"] ||= 'test'
+require File.expand_path("../../config/environment", __FILE__)
+require 'rspec/rails'
+require 'rspec/autorun'
+
+# Requires supporting ruby files with custom matchers and macros, etc,
+# in spec/support/ and its subdirectories.
+Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
+
+RSpec.configure do |config|
+ # ## Mock Framework
+ #
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
+ #
+ # config.mock_with :mocha
+ # config.mock_with :flexmock
+ # config.mock_with :rr
+
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
+
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
+ # examples within a transaction, remove the following line or assign false
+ # instead of true.
+ config.use_transactional_fixtures = true
+
+ # If true, the base class of anonymous controllers will be inferred
+ # automatically. This will be the default behavior in future versions of
+ # rspec-rails.
+ config.infer_base_class_for_anonymous_controllers = false
+
+ # Run specs in random order to surface order dependencies. If you find an
+ # order dependency and want to debug it, you can fix the order by providing
+ # the seed, which is printed after each run.
+ # --seed 1234
+ config.order = "random"
+
+ config.include FactoryGirl::Syntax::Methods
+ config.extend ControllerMacros, :type => :controller
+end
View
9 spec/support/controller_macros.rb
@@ -0,0 +1,9 @@
+module ControllerMacros
+ def login_user
+ before(:each) do
+ @request.env["devise.mapping"] = Devise.mappings[:user]
+ @logged_in_user = create(:user)
+ sign_in @logged_in_user
+ end
+ end
+end
View
3  spec/support/devise.rb
@@ -0,0 +1,3 @@
+RSpec.configure do |config|
+ config.include Devise::TestHelpers, :type => :controller
+end
View
0  test/fixtures/.gitkeep
No changes.
View
11 test/fixtures/comments.yml
@@ -1,11 +0,0 @@
-# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
-
-one:
- nickname: MyString
- message: MyText
- post_id: 1
-
-two:
- nickname: MyString
- message: MyText
- post_id: 1
View
11 test/fixtures/posts.yml
@@ -1,11 +0,0 @@
-# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
-
-one:
- title: MyString
- content: MyText
- user_id: 1
-
-two:
- title: MyString
- content: MyText
- user_id: 1
View
11 test/fixtures/users.yml
@@ -1,11 +0,0 @@
-# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
-
-# This model initially had no columns defined. If you add columns to the
-# model remove the '{}' from the fixture names and add the columns immediately
-# below each fixture, per the syntax in the comments below
-#
-one: {}
-# column: value
-#
-two: {}
-# column: value
View
0  test/functional/.gitkeep
No changes.
View
9 test/functional/comments_controller_test.rb
@@ -1,9 +0,0 @@
-require 'test_helper'
-
-class CommentsControllerTest < ActionController::TestCase
- test "should get create" do
- get :create
- assert_response :success
- end
-
-end
View
24 test/functional/posts_controller_test.rb
@@ -1,24 +0,0 @@
-require 'test_helper'
-
-class PostsControllerTest < ActionController::TestCase
- test "should get index" do
- get :index
- assert_response :success
- end
-
- test "should get show" do
- get :show
- assert_response :success
- end
-
- test "should get new" do
- get :new
- assert_response :success
- end
-
- test "should get create" do
- get :create
- assert_response :success
- end
-
-end
View
0  test/integration/.gitkeep
No changes.
View
12 test/performance/browsing_test.rb
@@ -1,12 +0,0 @@
-require 'test_helper'
-require 'rails/performance_test_help'
-
-class BrowsingTest < ActionDispatch::PerformanceTest
- # Refer to the documentation for all available options
- # self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory]
- # :output => 'tmp/performance', :formats => [:flat] }
-
- def test_homepage
- get '/'
- end
-end
View
13 test/test_helper.rb
@@ -1,13 +0,0 @@
-ENV["RAILS_ENV"] = "test"
-require File.expand_path('../../config/environment', __FILE__)
-require 'rails/test_help'
-
-class ActiveSupport::TestCase
- # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
- #
- # Note: You'll currently still have to declare fixtures explicitly in integration tests
- # -- they do not yet inherit this setting
- fixtures :all
-
- # Add more helper methods to be used by all tests here...
-end
View
0  test/unit/.gitkeep
No changes.
View
7 test/unit/comment_test.rb
@@ -1,7 +0,0 @@
-require 'test_helper'
-
-class CommentTest < ActiveSupport::TestCase
- # test "the truth" do
- # assert true
- # end
-end
View
4 test/unit/helpers/comments_helper_test.rb
@@ -1,4 +0,0 @@
-require 'test_helper'
-
-class CommentsHelperTest < ActionView::TestCase
-end
View
4 test/unit/helpers/posts_helper_test.rb
@@ -1,4 +0,0 @@
-require 'test_helper'
-
-class PostsHelperTest < ActionView::TestCase
-end
View
7 test/unit/post_test.rb
@@ -1,7 +0,0 @@
-require 'test_helper'
-
-class PostTest < ActiveSupport::TestCase
- # test "the truth" do
- # assert true
- # end
-end
View
7 test/unit/user_test.rb
@@ -1,7 +0,0 @@
-require 'test_helper'
-
-class UserTest < ActiveSupport::TestCase
- # test "the truth" do
- # assert true
- # end
-end

No commit comments for this range

Something went wrong with that request. Please try again.