如何使用RSpec

时间:2017-02-09 09:12:39

标签: ruby-on-rails ruby rspec devise

我真的很讨厌这个问题。

错误是这样的。 尽管声明了TestLoginController,但我总是遇到同样的错误。 我该怎么办?

$ bin/rspec
Running via Spring preloader in process 18113
/Users/noriakitakamizawa/dola/spec/controller/test_login_spec.rb:3:in `<top (required)>': uninitialized constant TestLoginController (NameError)
    from /Users/noriakitakamizawa/dola/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files'
    from /Users/noriakitakamizawa/dola/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `each'
    from /Users/noriakitakamizawa/dola/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `load_spec_files'

这是代码。

规格/控制器/ test_login_spec.rb

require 'rails_helper'

describe TestLoginController do
  login_admin

  it "should have a current_user" do
    # note the fact that you should remove the "validate_session" parameter if this was a scaffold-generated controller
    expect(subject.current_user).to_not eq(nil)
  end

  it "should get index" do
    # Note, rails 3.x scaffolding may add lines like get :index, {}, valid_session
    # the valid_session overrides the devise login. Remove the valid_session from your specs
    get 'index'
    response.should be_success
  end
end

规格/ rails_helper.rb

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 'devise'
require 'rspec/autorun'

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.include Devise::Test::ControllerHelpers, :type => :controller

  # 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

这是另一个帮手。

规格/ spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)

require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails'
require 'devise'
Rails.logger = Logger.new(STDOUT)

RSpec.configure do |config|
  config.include Rails.application.routes.url_helpers
  config.include Capybara::DSL

  # for Devise
  #config.include Devise::TestHelpers, :type => :controller
  #config.include Capybara::DSL
  config.include Devise::Test::ControllerHelpers, :type => :controller

0 个答案:

没有答案