gpt4 book ai didi

ruby-on-rails - 在 RoR 应用程序中使用 RSpec 进行测试 - 未初始化的常量

转载 作者:行者123 更新时间:2023-12-04 16:48:07 24 4
gpt4 key购买 nike

我正在尝试通过 RSpec 测试我的 Rails 3.2.3 应用程序。它已安装(我已经测试了另一个应用程序并且运行良好)但它不存在于 Gemfile 中。这是spec/application_controller_spec.rb的代码

    require "rspec"
require_relative "../app/controllers/application_controller"

describe ApplicationController do
it "current_cart does something" do
#app_controller = ApplicationController.new
pending
end
end

以下命令返回错误:
alex@ubuntu:~/RubymineProjects/psg$ rspec spec
/home/alex/RubymineProjects/psg/app/controllers/application_controller.rb:1:in `<top (required)>': uninitialized constant ActionController (NameError)
from /home/alex/RubymineProjects/psg/spec/application_controller_spec.rb:2:in `require_relative'
from /home/alex/RubymineProjects/psg/spec/application_controller_spec.rb:2:in `<top (required)>'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

文件 ApplicationController.rb
 class ApplicationController < ActionController::Base
def some_action
#............
end

结尾

即使我添加 gem 'rspec'在 Gemfile 中它不会改变任何东西,错误仍然存​​在。

有什么想法吗?

最佳答案

就我而言,我需要添加 rails_helper到规范文件或 .rspec文件。

require 'spec_helper'
require 'rails_helper'

describe SomeController do
end
# Or in the .rspec file:
--color
--require spec_helper
--require rails_helper

关于ruby-on-rails - 在 RoR 应用程序中使用 RSpec 进行测试 - 未初始化的常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11879107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com