ruby-on-rails - 除非明确添加 :type => :controller,否则 RSpec Controller 规范不起作用

标签 ruby-on-rails ruby-on-rails-4 rspec rspec-rails rspec3

我正在尝试运行 $ rspec命令,但我所有的 Controller 测试都失败了,除非我明确添加 :type => :controller到每个规范。

这是我得到的错误:

1) AccountsController GET index assigns all accounts as @accounts
     Failure/Error: get :index, {}, valid_session
     NoMethodError:
       undefined method `get' for #<RSpec::ExampleGroups::AccountsController_2::GETIndex:0x007fd96c8a6a68>
     # ./spec/controllers/accounts_controller_spec.rb:36:in `block (3 levels) in <top (required)>'

这是生成的规范:
require 'spec_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator.  If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails.  There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec.  Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.

describe AccountsController do

  # This should return the minimal set of attributes required to create a valid
  # Account. As you add validations to Account, be sure to
  # adjust the attributes here as well.
  let(:valid_attributes) { { "subdomain" => "MyString" } }

  # This should return the minimal set of values that should be in the session
  # in order to pass any filters (e.g. authentication) defined in
  # AccountsController. Be sure to keep this updated too.
  let(:valid_session) { {} }

  describe "GET index" do
    it "assigns all accounts as @accounts" do
      account = Account.create! valid_attributes
      get :index, {}, valid_session
      assigns(:accounts).should eq([account])
    end
  end
...

如果我查看 spec/rails_helper.rb 文件,我会看到这个片段:
# 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!

但是我是 RSpec 和 TDD 的新手,我不确定这个 rails_helper.rb 文件是否正在被读取。

规范/spec_helper.rb
# 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 'email_spec'
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|
  config.include(EmailSpec::Helpers)
  config.include(EmailSpec::Matchers)
  # ## 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.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end
  config.before(:each) do
    DatabaseCleaner.start
  end
  config.after(:each) do
    DatabaseCleaner.clean
  end
end

谁能告诉我如何让我的 Controller 规范运行而不必明确添加 :type => :controller ? Controller 规范都在目录 spec/controllers 中所以 RSpec 应该只知道它们是 Controller 规范。

最佳答案

您正在运行 RSpec 3,但尚未从 RSpec 2 完全升级您的配置。

rspec-rails 3 生成“spec_helper.rb”和“rails_helper.rb”。 “rails_helper.rb”需要“spec_helper.rb”。在 rspec-rails 3 中,Rails 类的规范应该 require 'rails_helper' ,不是 require 'spec_helper'正如生成的规范所做的那样。所以,要么

  • require 'spec_helper'require 'rails_helper' ,
  • 重新生成您的脚手架(我相信您的规范是由 rspec-rails 2 生成的),或
  • 完全删除要求并放入 --require rails_helper在您项目的 .rspec 中文件,因此它会自动包含在所有规范中。

  • 然后config.infer_spec_type_from_file_location!将生效。

    关于ruby-on-rails - 除非明确添加 :type => :controller,否则 RSpec Controller 规范不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24102132/

    相关文章:

    ruby - 在 Rspec 中测试非静态方法?

    ruby-on-rails - --RSpec 中的种子选项

    mysql - 使用 seed_dump gem 获取所有模型的 ID

    c# - Ruby on Rails TCPSocket在使用获取循环时卡住在客户端应用程序中

    ruby-on-rails - 有条件地应用带有 :if => condition in rails 4 的 skip_before_filter

    ruby-on-rails - 如何在 Spree 2.x/Rails 4 中覆盖 product_url 以使 SEO 更友好?

    csv - 生成伪造的 CSV 以使用 rspec 进行测试

    ruby-on-rails - Google Chrome浏览器未缓存图像

    ruby-on-rails - 为什么 application.rb 在 Rails 4.1 和 4.0 之间不同

    ruby-on-rails - RSpec 中的虚拟 ActiveRecord 模型