ruby-on-rails - NameError: unitialized constant ServiceName with default rspec configuration
问题描述
I created rails new --api whatever
with:
gem 'rspec-rails', '~> 3.8'
in my Gemfile
. Then, I created:
app/services/whatever_module/whatever_class.rb
and corresponding spec file:
spec/services/whatever_module/whatever_class_spec.rb
Now, when I run:
rspec services
I get this error:
NameError: uninitialized constant WhateverModule
How do I tell rspec to recognize the module by its spec path?
解决方案
您的规范文件应该在
spec/services/distamce/whatever_class_spec.rb
.
在您的情况下, rspec 尝试查找 WhatModule 因为/whatever_module/
在您的规范文件路径中。您可以尝试将此更改为spec/services/foo_bar/whatever_class_spec.rb
,您将收到缺少的 FooBarModule 错误。
我想我知道你错过了什么。
Rspec 不会自动要求您的 app 文件夹,因此最初 app 文件夹中没有可用的模块或类。
当您检查https://github.com/rspec/rspec-rails#installation @2 时,您可以看到您必须为 rspec 添加一些样板文件,例如rails_helper.rb
和spec_helper.rb
with rails generate rspec:install
。这些负责所有与 rspec 相关的设置和 app 文件夹的要求。
还需要require 'rails_helper'
在每个规范文件之上添加。
完成所有这些并收到错误Unable to autoload constant WhateverModule::WhateverClass
后,您whatever_class.rb
必须看起来像这样
module WhateverModule
class WhateverClass
end
end
或者您在文件夹之外的文件中定义模块whatever_module
。
推荐阅读
- laravel - 使用 Laravel 标记动态创建的复选框已选中
- xslt - 删除 xslt 中没有主体的节点
- substrate - 当事务失败时,是否可以在 Substrate UI 中读取 SRML 错误消息?
- jquery - 选择大小时添加 opacity .4 类,并且在加载函数 opacity 返回 1 后
- bash - 解析lua文件的脚本
- chef-infra - 使用 Policyfiles 和 ChefSpec 测试资源说明书
- javascript - 什么首先触发了本机反应?
- docker - 为什么 docker-compose build 不能运行命令,但 docker build 可以?
- jspdf - 使用 jspdf-autoTable 和 jspdf 复制表格的样式
- java - java - 如何在java spring mvc应用程序中防止服务器端的xss?