Fixed rspec warning info, refactor spec

This commit is contained in:
yafeilee 2016-08-07 00:44:11 +08:00
parent 0132f5c1d4
commit d75b5fce18
22 changed files with 206 additions and 90 deletions

4
.rspec
View File

@ -1,2 +1,2 @@
--colour
--color
--require spec_helper

View File

@ -65,7 +65,7 @@ end
group :test, :development do
gem "rspec-rails", ">= 2.8.1"
gem 'rails-controller-testing'
gem 'byebug'
gem 'factory_girl_rails'
gem 'rails-controller-testing'
end

View File

@ -128,7 +128,7 @@ GEM
guard-rails (0.8.0)
guard (~> 2.11)
guard-compat (~> 1.0)
guard-rspec (4.7.2)
guard-rspec (4.7.3)
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
@ -160,7 +160,7 @@ GEM
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mimemagic (0.3.1)
mimemagic (0.3.2)
mina (0.3.8)
open4 (~> 1.3.4)
rake
@ -184,7 +184,7 @@ GEM
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
notiffany (0.1.0)
notiffany (0.1.1)
nenv (~> 0.1)
shellany (~> 0.0)
open4 (1.3.4)
@ -246,7 +246,7 @@ GEM
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.1)
rspec-core (3.5.2)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
@ -266,7 +266,7 @@ GEM
rspec (~> 3.0, >= 3.0.0)
sidekiq (>= 2.4.0)
rspec-support (3.5.0)
ruby_dep (1.3.1)
ruby_dep (1.4.0)
sass (3.4.22)
sass-rails (5.0.6)
railties (>= 4.0.0, < 6)
@ -302,7 +302,7 @@ GEM
sprockets (3.7.0)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-es6 (0.9.0)
sprockets-es6 (0.9.1)
babel-source (>= 5.8.11)
babel-transpiler
sprockets (>= 3.0.0)
@ -314,12 +314,12 @@ GEM
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.5)
turbolinks (5.0.0)
turbolinks (5.0.1)
turbolinks-source (~> 5)
turbolinks-source (5.0.0)
tzinfo (1.2.2)
thread_safe (~> 0.1)
uglifier (3.0.0)
uglifier (3.0.1)
execjs (>= 0.3.0, < 3)
unf (0.1.4)
unf_ext

View File

@ -53,7 +53,7 @@ You can run it like a Ruby on Rails project as usual:
```shell
ruby -v
# 2.2.3
# 2.3.1
postgres --version
# 9.x.x
```
@ -106,8 +106,8 @@ You can read WBlog wiki for more information: [WBlog 的发布流程(Chinese onl
### Stack
* Ruby on Rails 5.0.0.beta3
* Ruby 2.2.3
* Ruby on Rails 5.0.0
* Ruby 2.3.1
* Turbolinks / SJR
* Foundation 6
* mina

View File

@ -75,7 +75,7 @@ WBlog 采用了 `mina` 作为自动化发布工具, 使用 `nginx`, `puma` 为
### 技术栈
* Ruby on Rails 5.0.0
* Ruby 2.2.3
* Ruby 2.3.1
* Foundation 6
* mina
* slim

View File

@ -1,5 +1,7 @@
WBlog::Application.routes.draw do
get 'hello/test'
resources :blogs, :only=>[:index, :show, :edit] do
collection do
get :rss

View File

@ -1,4 +1,4 @@
require 'spec_helper'
require 'rails_helper'
describe Admin::CommentsController do
RSpec.describe Admin::CommentsController, type: :controller do
end

View File

@ -1,6 +1,6 @@
require 'spec_helper'
require 'rails_helper'
describe Admin::DashboardController do
RSpec.describe Admin::DashboardController, type: :controller do
before do
session[:login] = true

View File

@ -1,6 +1,6 @@
require 'spec_helper'
require 'rails_helper'
describe Admin::PostsController do
RSpec.describe Admin::PostsController, type: :controller do
before do
session[:login] = true
@ -8,14 +8,14 @@ describe Admin::PostsController do
it "preview should return ok" do
post :preview
expect(response.body).to eq("")
post :preview, content: '123'
post :preview, params: { content: '123' }
expect(response.body).to eq("<p>123</p>\n")
end
it "update" do
post = create(:post)
patch 'update', id: post.id, labels: 'think, go '
patch 'update', params: { id: post.id, labels: 'think, go ' }
expect(post.reload.labels.size).to eq(2)
end
@ -27,21 +27,21 @@ describe Admin::PostsController do
post.save!
expect(label.posts.size).to eq(1)
delete 'destroy', id: post.id
delete 'destroy', params: { id: post.id }
expect( Post.all.size ).to eq(0)
expect( label.reload.posts.size ).to eq(0)
end
it "create" do
post_params = attributes_for(:post)
post 'create', post_params.merge( labels: 'think, go ' )
post 'create', params: post_params.merge( labels: 'think, go ' )
post = Post.first
expect( post.labels.size ).to eq(2)
end
it "create fail and see labels_content" do
post 'create', labels: 'think, go '
post 'create', params: { labels: 'think, go ' }
expect( assigns(:post).labels_content ).to eq('think, go')
end
end

View File

@ -1,6 +1,6 @@
require 'spec_helper'
require 'rails_helper'
describe Admin::SessionsController do
RSpec.describe Admin::SessionsController, type: :controller do
before do
session[:login] = true

View File

@ -1,4 +1,4 @@
require 'spec_helper'
require 'rails_helper'
RSpec.describe Admin::SubscribesController, :type => :controller do
@ -16,7 +16,7 @@ RSpec.describe Admin::SubscribesController, :type => :controller do
describe "POST enable" do
it "returns http success" do
subscribe = create(:subscribe)
post :enable, id: subscribe.id
post :enable, params: { id: subscribe.id }
expect(subscribe.reload.enable).to eq(true)
end
end
@ -24,7 +24,7 @@ RSpec.describe Admin::SubscribesController, :type => :controller do
describe "POST disable" do
it "returns http success" do
subscribe = create(:subscribe, enable: true)
post :disable, id: subscribe.id
post :disable, params: { id: subscribe.id }
expect(subscribe.reload.enable).to eq(false)
end
end

View File

@ -1,6 +1,6 @@
require 'spec_helper'
require 'rails_helper'
describe ArchivesController do
RSpec.describe ArchivesController, type: :controller do
it "get index" do
create_list(:post_list, 3)

View File

@ -1,6 +1,6 @@
require 'spec_helper'
require 'rails_helper'
describe BlogsController do
RSpec.describe BlogsController, type: :controller do
describe 'get INDEX' do
it 'index should get by order desc' do
@ -27,7 +27,7 @@ describe BlogsController do
comment2.post = post
comment2.save!
get :show, id: post.id
get :show, params: { id: post.id }
expect(assigns(:comments)[0]).to eq(comment2)
expect(assigns(:comments)[1]).to eq(comment1)
end
@ -38,26 +38,26 @@ describe BlogsController do
selected = posts[1]
s_prev = posts[0]
s_next = posts[2]
get :show, id: selected.id
get :show, params: { id: selected.id }
expect(assigns(:prev)).to eq(s_prev)
expect(assigns(:next)).to eq(s_next)
# 下界
selected = posts[0]
get :show, id: selected.id
get :show, params: { id: selected.id }
expect(assigns(:prev)).to be_nil
expect(assigns(:next)).to eq(posts[1])
# 测试上界
selected = posts[2]
get :show, id: selected.id
get :show, params: { id: selected.id }
expect(assigns(:prev)).to eq(posts[1])
expect(assigns(:next)).to be_nil
# 测试未来时间
create(:post, created_at: Time.now + 100)
selected = posts[1]
get :show, id: selected.id
get :show, params: { id: selected.id }
expect(assigns(:prev)).to eq(posts[0])
expect(assigns(:next)).to eq(posts[2])
end

View File

@ -1,6 +1,6 @@
require 'spec_helper'
require 'rails_helper'
describe HomeController do
RSpec.describe HomeController, type: :controller do
describe "GET 'index'" do
it "returns http success" do

View File

@ -1,20 +1,20 @@
require 'spec_helper'
require 'rails_helper'
describe LikesController do
RSpec.describe LikesController, type: :controller do
it "get index" do
a = Post.create!(title: 'one', content: '1'*31)
get 'index', blog_id: a.id
get 'index', params: { blog_id: a.id }
expect(JSON.parse(response.body)['count']).to eq(0)
a.likes << Like.new
a.save!
get 'index', blog_id: a.id
get 'index', params: { blog_id: a.id }
expect(JSON.parse(response.body)['count']).to eq(1)
end
it "post create" do
a = Post.create!(title: 'one', content: '1'*31)
post 'create', blog_id: a.id
post 'create', params: { blog_id: a.id }
expect(a.likes.size).to eq(1)
end
@ -23,7 +23,7 @@ describe LikesController do
like = Like.new
a.likes << like
a.save!
delete 'destroy', blog_id: a.id, id: like.id
delete 'destroy', params: { blog_id: a.id, id: like.id }
expect(a.reload.likes.size).to eq(0)
end
end

View File

@ -1,16 +1,16 @@
require 'spec_helper'
require 'rails_helper'
describe SubscribesController do
RSpec.describe SubscribesController, type: :controller do
describe "POST 'create'" do
it "post ok" do
post 'create', { subscribe: { email: 'tester@test.com' } }
post 'create', params: { subscribe: { email: 'tester@test.com' } }
expect(Subscribe.all.size).to eq(1)
end
it "post with disabled email" do
subscribe = Subscribe.create(email: 'tester@test.com', enable: false)
post 'create', { subscribe: { email: 'tester@test.com' } }
post :create, params: { subscribe: { email: 'tester@test.com' } }
expect(subscribe.reload.enable).to be_truthy
end
end

View File

@ -1,11 +1,11 @@
require 'spec_helper'
require 'rails_helper'
describe UnsubscribesController do
RSpec.describe UnsubscribesController, type: :controller do
describe "POST 'create'" do
it "unsuscribe ok" do
subscribe = create(:subscribe, enable: true)
post 'create', { subscribe: { email: subscribe.email } }
post 'create', params: { subscribe: { email: subscribe.email } }
expect(subscribe.reload.enable).to be(false)
end
end

View File

@ -1,6 +1,6 @@
require 'spec_helper'
require 'rails_helper'
describe Like do
RSpec.describe Like, type: :model do
it "add like" do
a = Post.create!(title: 'one', content: '1'*31)
like = Like.new

View File

@ -1,6 +1,6 @@
require 'spec_helper'
require 'rails_helper'
describe Post do
RSpec.describe Post, type: :model do
it "validates should be ok" do
expect(create(:post)).to be_truthy
end

View File

@ -1,5 +1,6 @@
require 'spec_helper'
describe Subscribe do
require 'rails_helper'
RSpec.describe Subscribe, type: :model do
it "validates should be ok" do
expect(create(:subscribe)).to be_truthy
end

50
spec/rails_helper.rb Normal file
View File

@ -0,0 +1,50 @@
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'codeclimate-test-reporter'
CodeClimate::TestReporter.start
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
RSpec::Sidekiq.configure do |config|
config.warn_when_jobs_not_processed_by_sidekiq = false
end

View File

@ -1,36 +1,99 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require "codeclimate-test-reporter"
CodeClimate::TestReporter.start
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# 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}
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
config.include FactoryGirl::Syntax::Methods
config.before :each do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.start
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.after :each do
DatabaseCleaner.clean
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
config.mock_with :rspec
config.infer_base_class_for_anonymous_controllers = false
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups
[: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
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# This allows you to limit a spec run to individual examples or groups
# you care about by tagging them with `:focus` metadata. When nothing
# is tagged with `:focus`, all examples get run. RSpec also provides
# aliases for `it`, `describe`, and `context` that include `:focus`
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
config.filter_run_when_matching :focus
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
RSpec::Sidekiq.configure do |config|
config.warn_when_jobs_not_processed_by_sidekiq = false
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# 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
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end