Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 除非明确添加:type=>;,否则RSpec控制器规范不起作用:控制器_Ruby On Rails_Ruby On Rails 4_Rspec_Rspec Rails_Rspec3 - Fatal编程技术网

Ruby on rails 除非明确添加:type=>;,否则RSpec控制器规范不起作用:控制器

Ruby on rails 除非明确添加:type=>;,否则RSpec控制器规范不起作用:控制器,ruby-on-rails,ruby-on-rails-4,rspec,rspec-rails,rspec3,Ruby On Rails,Ruby On Rails 4,Rspec,Rspec Rails,Rspec3,我正在尝试运行$rspec命令,但是我的所有控制器测试都失败,除非我明确地将:type=>:controller添加到每个规范中 下面是我得到的错误: 1) AccountsController GET index assigns all accounts as @accounts Failure/Error: get :index, {}, valid_session NoMethodError: undefined method `get' for #<

我正在尝试运行
$rspec
命令,但是我的所有控制器测试都失败,除非我明确地将
: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)>'
如果查看spec/rails\u 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\u helper.rb文件是否正在读取

spec/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

有谁能告诉我如何在不显式添加
:type=>:controller
的情况下运行我的控制器规范?控制器规格都在dir
spec/controllers
中,因此RSpec应该知道它们是控制器规格。

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

rspec rails 3生成“spec\u helper.rb”和“rails\u helper.rb”rails\u helper.rb'需要'spec\u helper.rb'。在rspec rails 3中,rails类的规范应该
要求“rails\u helper”
,而不是
要求“spec\u helper”
,就像生成的规范那样。所以,也

  • require'spec\u helper'
    更改为
    require'rails\u helper'
  • 重新生成脚手架(我相信您的规范是由rspec rails 2生成的),或者
  • 完全删除require并将
    --require rails\u helper
    放在项目的
    .rspec
    文件中,以便它自动包含在所有规范中

然后从文件位置推断规格类型将生效。

这似乎让我克服了那个特定的错误。谢谢
# 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