Permalink
Browse files

Removes all old unit tests skeletons. Adds rspec_rails + shoulda + fa…

…ctory_girl_rails + simple_cov. Adds tests for current 100% coverage
  • Loading branch information...
1 parent ffb1862 commit e57afac8cdb5fffae90a7d201a9e3352077d1ce2 Javier Fonseca V committed Nov 26, 2012
View
@@ -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
@@ -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)
@@ -14,4 +14,8 @@ staging:
production:
<<: *defaults
- database: my_blog_production
+ database: my_blog_production
+
+test:
+ <<: *defaults
+ database: my_blog_test
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,3 @@
+RSpec.configure do |config|
+ config.include Devise::TestHelpers, :type => :controller
+end
View
No changes.
View
@@ -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
Oops, something went wrong.

0 comments on commit e57afac

Please sign in to comment.