Fix test cases

This commit is contained in:
yafeilee 2016-04-25 12:27:32 +08:00
parent 6ec662459f
commit 33b7f6666d
14 changed files with 42 additions and 74 deletions

View File

@ -67,7 +67,8 @@ end
group :test, :development do group :test, :development do
gem "rspec-rails", ">= 2.8.1" gem "rspec-rails", ">= 2.8.1"
gem 'rails-controller-testing'
gem 'pry-rails' gem 'pry-rails'
gem 'pry-nav' gem 'pry-nav'
#gem 'factory_girl_rails' gem 'factory_girl_rails'
end end

View File

@ -86,6 +86,11 @@ GEM
unf (>= 0.0.5, < 1.0.0) unf (>= 0.0.5, < 1.0.0)
erubis (2.7.0) erubis (2.7.0)
execjs (2.6.0) execjs (2.6.0)
factory_girl (4.7.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.7.0)
factory_girl (~> 4.7.0)
railties (>= 3.0.0)
ffi (1.9.10) ffi (1.9.10)
figaro (1.1.1) figaro (1.1.1)
thor (~> 0.14) thor (~> 0.14)
@ -200,6 +205,10 @@ GEM
bundler (>= 1.3.0, < 2.0) bundler (>= 1.3.0, < 2.0)
railties (= 5.0.0.beta3) railties (= 5.0.0.beta3)
sprockets-rails (>= 2.0.0) sprockets-rails (>= 2.0.0)
rails-controller-testing (0.1.1)
actionpack (~> 5.x)
actionview (~> 5.x)
activesupport (~> 5.x)
rails-deprecated_sanitizer (1.0.3) rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha) activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.7) rails-dom-testing (1.0.7)
@ -331,6 +340,7 @@ DEPENDENCIES
codeclimate-test-reporter codeclimate-test-reporter
coffee-rails (~> 4.1.0) coffee-rails (~> 4.1.0)
database_cleaner database_cleaner
factory_girl_rails
figaro figaro
font-awesome-sass font-awesome-sass
foundation-icons-sass-rails foundation-icons-sass-rails
@ -359,6 +369,7 @@ DEPENDENCIES
quiet_assets quiet_assets
rack-cors rack-cors
rails (>= 5.0.0.beta3, < 5.1) rails (>= 5.0.0.beta3, < 5.1)
rails-controller-testing
rails-i18n (~> 5.0.0.beta3) rails-i18n (~> 5.0.0.beta3)
redcarpet redcarpet
redis-namespace redis-namespace

View File

@ -13,8 +13,8 @@ WBlog::Application.configure do
config.eager_load = false config.eager_load = false
# Configure static asset server for tests with Cache-Control for performance. # Configure static asset server for tests with Cache-Control for performance.
config.serve_static_files = true config.public_file_server.enabled = true
config.static_cache_control = "public, max-age=3600" config.public_file_server.headers = { 'Cache-Control' => 'public, max-age=3600' }
# Show full error reports and disable caching. # Show full error reports and disable caching.
config.consider_all_requests_local = true config.consider_all_requests_local = true

View File

@ -0,0 +1,5 @@
class ChangeColumnDefaultEnableSubscribes < ActiveRecord::Migration[5.0]
def change
change_column_default :subscribes, :enable, true
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160421062614) do ActiveRecord::Schema.define(version: 20160425042136) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -58,7 +58,7 @@ ActiveRecord::Schema.define(version: 20160421062614) do
create_table "subscribes", force: :cascade do |t| create_table "subscribes", force: :cascade do |t|
t.string "email" t.string "email"
t.boolean "enable" t.boolean "enable", default: true
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end

View File

@ -2,48 +2,10 @@ require 'spec_helper'
describe ArchivesController do describe ArchivesController do
def posts_data(response) it "get index" do
JSON.parse(response.body)["posts"] create_list(:post_list, 3)
end get :index
expect(response.status).to eq(200)
describe "GET 'index'" do
it "json" do
get 'index', format: :json
expect(posts_data(response).size).to eq(0)
end
it "add ten" do
posts = create_list(:post_list, 10)
get 'index', format: :json
expect(posts_data(response).size).to eq(10)
end
it "add 20" do
posts = create_list(:post_list, 20)
get 'index', format: :json
expect(posts_data(response).size).to eq(10)
end
it "type filter" do
posts = create_list(:post_list, 20)
life_posts = create_list(:post_list, 5, type: Post::LIFE)
get 'index', type: 'life', format: :json
expect(posts_data(response).size).to eq(5)
end
it "start_with filter" do
posts = create_list(:post_list, 10)
start_with = posts[4].created_at.to_i.to_s
get 'index', start_with: start_with, format: :json
expect(posts_data(response).size).to eq(5)
end
it "load_more" do
posts = create_list(:post_list, 20)
get 'index', format: :json
start_with = JSON.parse(response.body)['start_with']
expect(start_with).to eq( posts[9].created_at.to_i.to_s )
end
end end
end end

View File

@ -9,7 +9,7 @@ describe BlogsController do
first = Post.first first = Post.first
first.update(title: 'first') first.update(title: 'first')
second = Post.desc(:created_at)[1] second = Post.order(created_at: :desc)[1]
get :index get :index
expect(assigns(:newest)).to eq(first) expect(assigns(:newest)).to eq(first)
expect(assigns(:recent)[0]).to eq(second) expect(assigns(:recent)[0]).to eq(second)
@ -28,13 +28,13 @@ describe BlogsController do
comment2.save! comment2.save!
get :show, id: post.id get :show, id: post.id
expect(assigns(:comments)[0]).to eq(comment1) expect(assigns(:comments)[0]).to eq(comment2)
expect(assigns(:comments)[1]).to eq(comment2) expect(assigns(:comments)[1]).to eq(comment1)
end end
it "#prev, #next" do it "#prev, #next" do
posts = create_list(:post_list, 3) posts = create_list(:post_list, 3)
posts = Post.order_by(created_at: 'asc') posts = Post.order(created_at: :asc)
selected = posts[1] selected = posts[1]
s_prev = posts[0] s_prev = posts[0]
s_next = posts[2] s_next = posts[2]

View File

@ -3,7 +3,7 @@ require 'spec_helper'
describe LikesController do describe LikesController do
it "get index" do it "get index" do
a = Post.create!(title: 'one', content: '1'*31, type: Post::TECH ) a = Post.create!(title: 'one', content: '1'*31)
get 'index', blog_id: a.id get 'index', blog_id: a.id
expect(JSON.parse(response.body)['count']).to eq(0) expect(JSON.parse(response.body)['count']).to eq(0)
a.likes << Like.new a.likes << Like.new
@ -13,13 +13,13 @@ describe LikesController do
end end
it "post create" do it "post create" do
a = Post.create!(title: 'one', content: '1'*31, type: Post::TECH ) a = Post.create!(title: 'one', content: '1'*31)
post 'create', blog_id: a.id post 'create', blog_id: a.id
expect(a.likes.size).to eq(1) expect(a.likes.size).to eq(1)
end end
it "DELETE destroy" do it "DELETE destroy" do
a = Post.create!(title: 'one', content: '1'*31, type: Post::TECH ) a = Post.create!(title: 'one', content: '1'*31)
like = Like.new like = Like.new
a.likes << like a.likes << like
a.save! a.save!

View File

@ -2,13 +2,11 @@ FactoryGirl.define do
factory :post do factory :post do
title 'this is a post title' title 'this is a post title'
content 'content' * 10 content 'content' * 10
type Post::TECH
end end
factory :post_list, class: Post do factory :post_list, class: Post do
sequence(:title) { |n| "#{n}: post title" } sequence(:title) { |n| "#{n}: post title" }
content 'content' * 10 content 'content' * 10
sequence(:created_at) { |n| n.days.ago } sequence(:created_at) { |n| n.days.ago }
type Post::TECH
end end
end end

View File

@ -1,5 +0,0 @@
require 'rails_helper'
RSpec.describe Comment, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -1,5 +0,0 @@
require 'rails_helper'
RSpec.describe Label, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -2,7 +2,7 @@ require 'spec_helper'
describe Like do describe Like do
it "add like" do it "add like" do
a = Post.create!(title: 'one', content: '1'*31, type: Post::TECH ) a = Post.create!(title: 'one', content: '1'*31)
like = Like.new like = Like.new
like.post = a like.post = a
expect(like.save).to eq(true) expect(like.save).to eq(true)

View File

@ -1,5 +0,0 @@
require 'rails_helper'
RSpec.describe Photo, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -23,6 +23,12 @@ RSpec.configure do |config|
config.mock_with :rspec config.mock_with :rspec
config.infer_base_class_for_anonymous_controllers = false config.infer_base_class_for_anonymous_controllers = false
[:controller, :view, :request].each do |type|
config.include ::Rails::Controller::Testing::TestProcess, :type => type
config.include ::Rails::Controller::Testing::TemplateAssertions, :type => type
config.include ::Rails::Controller::Testing::Integration, :type => type
end
end end
RSpec::Sidekiq.configure do |config| RSpec::Sidekiq.configure do |config|