我已经升级了rails 4.2.0和spree 3.0
一旦我运行 rspec,我就会收到以下错误,它向我显示 Rails 4.2.0 中的 rspec ActionController::UrlGenerationError,我在 google 上搜索了很多,但没有找到任何解决方案,请找到以下日志:
Run options: include {:locations=>{"./spec/controllers/messages_controller_spec.rb"=>[10]}}
F
Failures:
1) MessagesController create with valid message sends message and shows flash notice
Failure/Error: get "contact-us"
ActionController::UrlGenerationError:
No route matches {:action=>"contact-us", :controller=>"messages"}
# /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_dispatch/journey/formatter.rb:46:in `generate'
# /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:727:in `generate'
# /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:758:in `generate'
# /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:753:in `generate_extras'
# /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:748:in `extra_keys'
# /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:208:in `assign_parameters'
# /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:619:in `process'
# /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:65:in `process'
# /home/rails22/.rvm/gems/ruby-2.2.0@/gems/devise-3.4.1/lib/devise/test_helpers.rb:19:in `block in process'
# /home/rails22/.rvm/gems/ruby-2.2.0@/gems/devise-3.4.1/lib/devise/test_helpers.rb:72:in `catch'
# /home/rails22/.rvm/gems/ruby-2.2.0@/gems/devise-3.4.1/lib/devise/test_helpers.rb:72:in `_catch_warden'
# /home/rails22/.rvm/gems/ruby-2.2.0@/gems/devise-3.4.1/lib/devise/test_helpers.rb:19:in `process'
# /home/rails22/.rvm/gems/ruby-2.2.0@/gems/actionpack-4.2.1/lib/action_controller/test_case.rb:508:in `get' …Run Code Online (Sandbox Code Playgroud) 我如何要求 rspec 在下一个请求中使用/保持上一个请求所用的会话。
如果我在第一次获取后打印会话,它与我在处理 blah2_path 的操作中看到的会话不同。
get blah_path, {}, headers
get blah2_path, {}, headers # use same session as blah2_path
Run Code Online (Sandbox Code Playgroud) 我试图掌握 TDD 的一些概念,在我的 RoR 应用程序中,我有 /about 视图,它属于 static_pages#about。它在routes.rb中定义了路由get 'about' => 'static_pages#about'。到目前为止,一切都可以在浏览器中运行,但我也想通过 RSpec 来测试它。给定
RSpec.describe "about.html.erb", type: :view do
it "renders about view" do
render :template => "about"
expect(response).to render_template('/about')
end
end
Run Code Online (Sandbox Code Playgroud)
引发错误
Missing template /about with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :......
Run Code Online (Sandbox Code Playgroud)
谢谢!
首先,我正在使用:
我已经设置了一个带有多态模型的可安装 Rails 引擎,我想在其他引擎中使用它。模型类如下所示:
module Geo
class Location < ApplicationRecord
belongs_to :locatable, polymorphic: true
end
end
Run Code Online (Sandbox Code Playgroud)
在我的规范中,我想确保我有一个有效的模型,但是,在引擎中我没有与Geo::Location.
我如何设置一个虚拟类来测试有效性(belongs_to需要存在)或者您使用过哪些好的测试策略?
搜索此问题仅发现了其他人处理过的特定宝石或类别的问题。就我而言,我认为 rspec 总体上有问题。
每当我使用 生成控制器时rails generate ControllerName,它都会设置所有内容并且一切似乎工作正常。我已经路由了一些控制器并在开发和生产中测试了它们,一切正常。
唯一似乎被破坏的是用 rspec 测试它们。现在我的项目中有两个控制器,每当我运行 rspec/spec 时,大多数由 生成的规范都会rails g出现此错误:
NoMethodError:
undefined method `setup' for RSpec::ExampleGroups::MoreStuffHere
Run Code Online (Sandbox Code Playgroud)
例如,我有一个WelcomeController和ApplicationsController,这些是我不断收到的错误:
undefined method `setup' for RSpec::ExampleGroups::ApplicationsController:Class
undefined method `setup' for RSpec::ExampleGroups::WelcomeController:Class
undefined method `setup' for RSpec::ExampleGroups::WelcomeAboutHtmlErb:Class
undefined method `setup' for RSpec::ExampleGroups::WelcomeIndexHtmlErb:Class
Run Code Online (Sandbox Code Playgroud)
有趣的是,我没有收到他们的helper_spec错误
这是一个完整的错误,以防有帮助:
An error occurred while loading ./spec/controllers/applications_controller_spec.rb.
Failure/Error:
RSpec.describe ApplicationsController, type: :controller do
end
NoMethodError:
undefined method `setup' for RSpec::ExampleGroups::ApplicationsController:Class
# ./spec/controllers/applications_controller_spec.rb:3:in `<top (required)>'
Run Code Online (Sandbox Code Playgroud)
有谁知道这个问题可能出在哪里?
我正在开发一个具有 500 多个规格的应用程序,当我在本地计算机上运行这些规格时,它成功通过了。但当这些规范在 CircleCI 上运行时,该进程就会被终止。我尝试通过跟踪本地计算机上的内存来调查该问题。当我看到 ruby 进程占用了 4GB 内存时,我感到很惊讶,这就是触发 CircleCI 杀死该进程的原因。
我不确定我的规格占用所有这些内存的原因我已经搜索过在每个规格后清理内存的配置但无济于事。
这是我的rails_helper.rb
require "mongoid-rspec"
require "spec_helper"
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
if Rails.env.production?
abort("The Rails environment is running in production mode!")
end
require "database_cleaner"
require "rspec/rails"
#
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
# FactoryGirl
config.include FactoryGirl::Syntax::Methods
# Render
config.render_views
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
config.include Mongoid::Matchers, type: …Run Code Online (Sandbox Code Playgroud) 我正在使用 Pundit 在我的 Rails 应用程序上进行授权,并且正在对我的请求进行单元测试。我已经成功测试了该策略,但现在我想验证我的请求是否正在使用该策略。我想以通用的方式做到这一点,我可以在任何请求规范中使用它(无论控制器、操作和策略如何)。老实说,在这一点上,我会遵循一种对所有请求通用的“期望任何策略都能获得授权”的说法。
对于索引操作(使用策略范围)很简单:
在请求描述中我说
RSpec.describe 'GET /companies', type: :request do
include_context 'invokes policy scope'
end
Run Code Online (Sandbox Code Playgroud)
然后我将共享上下文定义为:
RSpec.shared_context 'invokes policy scope', shared_context: :metadata do
before do
expect(Pundit).to receive(:policy_scope!) do |user_context, relation|
expect(user_context.user).to eq(user)
expect(user_context.current_group).to eq(group)
relation
end
end
end
Run Code Online (Sandbox Code Playgroud)
但我该如何对授权方法做同样的事情呢?我不知道哪个具体策略将接收它,也不知道哪个具体控制器将调用authorize.
我不明白为什么 pundit 不提供自定义匹配器来验证某个请求实际上正在调用给定的策略,或者“模拟”授权/未授权的场景,因此我测试我的请求是否返回正确的状态代码。
有任何想法吗?
我将一个应用程序升级到 Rails 6.1.0(从 6.0.3.3 开始,通过创建一个新的仅 api 应用程序,添加 RSpec,然后手动复制所需的文件)。
当我运行 RSpec 时,我看到以下警告:
DEPRECATION WARNING: connection_config is deprecated and will be removed from Rails 6.2 (Use
connection_db_config instead) (called from <top (required)> at
[...app/models/application_record.rb:1].
Run Code Online (Sandbox Code Playgroud)
我没有更改ApplicationRecord默认的类:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
Run Code Online (Sandbox Code Playgroud)
我仅在运行 RSpec 时看到此警告。我没有在 Rails 控制台或 Rails 服务器日志中看到它。
这是我的config/database.yml:
default: &default
adapter: postgresql
encoding: unicode
host: <%= ENV.fetch('DATABASE_HOST', 'localhost') %>
username: <%= ENV.fetch('POSTGRES_USER', 'postgres') %>
password: <%= ENV.fetch('POSTGRES_PASSWORD', '') %>
database: <%= ENV.fetch('POSTGRES_DB', 'myapp_development') %>
pool: …Run Code Online (Sandbox Code Playgroud) rspec ruby-on-rails rspec-rails rails-activerecord money-rails
最近我更新了Ruby(2.5.3至2.7.1)和Ruby on Rails(5.2.2至6.1.1)版本。
之后,当我运行rspec时,出现此错误:
Failure/Error: require File.expand_path("../../config/environment", __FILE__)
NoMethodError:
undefined method `new' for BigDecimal:Class
# ./config/application.rb:11:in `<top (required)>'
# ./config/environment.rb:2:in `<top (required)>'
Run Code Online (Sandbox Code Playgroud)
Spec_helper.rb 中发生错误如何调试spec_helper 的代码,即:
require File.expand_path("../../config/environment", __FILE__)
Run Code Online (Sandbox Code Playgroud)
如何解决上述错误?我不确定哪个 gem/文件想要做什么BigDecimal.new
升级 Rails 后运行rspec会导致未初始化的常量错误:RSpec::Rails::Railtie::SourceAnnotationExtractor。
回溯指向“rails_helper.rb”文件。
我已经尝试过bundle update rspec-rails。
rspec ruby-on-rails uninitialized-constant rspec-rails ruby-on-rails-6.1