From 8bbb12e8da3529dd2a98b0c4500826f0107fd6d6 Mon Sep 17 00:00:00 2001 From: masarakki Date: Thu, 8 Oct 2015 22:58:21 +0900 Subject: [PATCH] fix for rspec in view tests of rspec, we must assign vars by myself. --- .rspec | 2 + Appraisals | 6 ++ Rakefile | 7 ++ gemfiles/rails_4.2_with_rspec.gemfile | 9 ++ lib/react/rails.rb | 1 + lib/react/rails/railtie.rb | 1 + lib/react/rails/rspec_helper.rb | 18 ++++ spec/controllers/pages_controller_spec.rb | 10 +++ spec/controllers/server_controller_spec.rb | 36 ++++++++ spec/rails_helper.rb | 11 +++ spec/spec_helper.rb | 96 ++++++++++++++++++++++ spec/views/pages/show.html.erb_spec.rb | 10 +++ 12 files changed, 207 insertions(+) create mode 100644 .rspec create mode 100644 gemfiles/rails_4.2_with_rspec.gemfile create mode 100644 lib/react/rails/rspec_helper.rb create mode 100644 spec/controllers/pages_controller_spec.rb create mode 100644 spec/controllers/server_controller_spec.rb create mode 100644 spec/rails_helper.rb create mode 100644 spec/spec_helper.rb create mode 100644 spec/views/pages/show.html.erb_spec.rb diff --git a/.rspec b/.rspec new file mode 100644 index 000000000..9d52c5632 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format doc diff --git a/Appraisals b/Appraisals index b34a43f43..333227255 100644 --- a/Appraisals +++ b/Appraisals @@ -18,3 +18,9 @@ end appraise "rails-4.2" do gem 'rails', '~> 4.2.1' end + +appraise "rails-4.2-with-rspec" do + gem 'rails', '~> 4.2.1' + gem 'rspec' + gem 'rspec-rails' +end diff --git a/Rakefile b/Rakefile index 2b33d7064..aa9d8f666 100644 --- a/Rakefile +++ b/Rakefile @@ -36,4 +36,11 @@ Rake::TestTask.new(:test) do |t| t.verbose = ENV['TEST_VERBOSE'] == '1' end +begin + require 'rspec/core/rake_task' + RSpec::Core::RakeTask.new + task test: :spec +rescue LoadError +end + task default: :test diff --git a/gemfiles/rails_4.2_with_rspec.gemfile b/gemfiles/rails_4.2_with_rspec.gemfile new file mode 100644 index 000000000..5b1f9da60 --- /dev/null +++ b/gemfiles/rails_4.2_with_rspec.gemfile @@ -0,0 +1,9 @@ +# This file was generated by Appraisal + +source "http://rubygems.org" + +gem "rails", "~> 4.2.1" +gem "rspec" +gem "rspec-rails" + +gemspec :path => "../" diff --git a/lib/react/rails.rb b/lib/react/rails.rb index a1a98507a..1eab6c617 100644 --- a/lib/react/rails.rb +++ b/lib/react/rails.rb @@ -6,3 +6,4 @@ require 'react/rails/component_mount' require 'react/rails/view_helper' require 'react/rails/controller_renderer' +require 'react/rails/rspec_helper' diff --git a/lib/react/rails/railtie.rb b/lib/react/rails/railtie.rb index c90d16672..8978fee97 100644 --- a/lib/react/rails/railtie.rb +++ b/lib/react/rails/railtie.rb @@ -83,6 +83,7 @@ class Railtie < ::Rails::Railtie React::ServerRendering.reset_pool # Reload renderers in dev when files change ActionDispatch::Reloader.to_prepare { React::ServerRendering.reset_pool } + RSpec.configure { |c| c.include React::Rails::RspecHelper } if defined?(RSpec::Rails) end end end diff --git a/lib/react/rails/rspec_helper.rb b/lib/react/rails/rspec_helper.rb new file mode 100644 index 000000000..57e9f5404 --- /dev/null +++ b/lib/react/rails/rspec_helper.rb @@ -0,0 +1,18 @@ +module React + module Rails + module RspecHelper + extend ActiveSupport::Concern + + included do |mod| + mod.class_eval do + before { setup_react_rails_rspec } if respond_to?(:before) + end + + def setup_react_rails_rspec + helper = controller.setup_react_component_helper if respond_to?(:controller) + assign :__react_component_helper, helper if helper && respond_to?(:assign) + end + end + end + end +end diff --git a/spec/controllers/pages_controller_spec.rb b/spec/controllers/pages_controller_spec.rb new file mode 100644 index 000000000..8efa1f858 --- /dev/null +++ b/spec/controllers/pages_controller_spec.rb @@ -0,0 +1,10 @@ +require 'rails_helper' + +RSpec.describe PagesController, type: :controller do + describe 'assigns' do + it do + get :show , id: 1 + expect(assigns).to have_key :__react_component_helper + end + end +end diff --git a/spec/controllers/server_controller_spec.rb b/spec/controllers/server_controller_spec.rb new file mode 100644 index 000000000..aa66f7dc0 --- /dev/null +++ b/spec/controllers/server_controller_spec.rb @@ -0,0 +1,36 @@ +require 'rails_helper' + +RSpec.describe ServerController, type: :controller do + describe 'console_example' do + it do + get :console_example + [/Console Logged/, + /console.log.apply\(console, \["got initial state"\]\)/, + /console.warn.apply\(console, \["mounted component"\]\)/, + /console.error.apply\(console, \["rendered!","foo"\]\)/].each do |regex| + expect(response.body).to match regex + end + end + end + + describe 'console_example_suppressed' do + it do + get :console_example_suppressed + expect(response.body).to match(/Console Logged/) + [/console.log/, /console.warn/, /console.error/].each do |regex| + expect(response.body).not_to match regex + end + end + end + + describe 'inline_component' do + it do + get :inline_component + [//, + /Dummy<\/title>/].each do |regex| + expect(response.body).to match regex + end + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 000000000..1ce40b83b --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,11 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +ENV['RAILS_ENV'] ||= 'test' +require 'spec_helper' +require File.expand_path('../../test/dummy/config/environment', __FILE__) +require 'rspec/rails' + +Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } + +RSpec.configure do |config| + config.render_views = true +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 000000000..6839d5f9d --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,96 @@ +# This file was generated by the `rspec --init` 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| + # 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 + + # 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 + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # These two settings work together to allow 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. + config.filter_run :focus + config.run_all_when_everything_filtered = true + + # 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://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching + config.disable_monkey_patching! + + # This setting enables warnings. It's recommended, but in some cases may + # be too noisy due to issues in dependencies. + config.warnings = true + + # 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 + + # 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 diff --git a/spec/views/pages/show.html.erb_spec.rb b/spec/views/pages/show.html.erb_spec.rb new file mode 100644 index 000000000..4ae1d8450 --- /dev/null +++ b/spec/views/pages/show.html.erb_spec.rb @@ -0,0 +1,10 @@ +require 'rails_helper' + +RSpec.describe 'pages/show.html.erb', type: :view do + describe 'assigns' do + it { expect(assigns).to have_key :__react_component_helper } + end + describe '#render' do + it { expect { render }.not_to raise_error } + end +end