标签: rubocop-rspec

RSpec:避免使用允许任何实例接收

我正在处理旧的代码部分。

before do
  allow_any_instance_of(SportRateManager)
    .to receive(:create)
    .and_return(true)
end
Run Code Online (Sandbox Code Playgroud)

有Rubocop错误,例如:

避免使用'allow_any_instance_of'进行存根

我读到有关RuboCop :: RSpec:AnyInstance的内容,并试图像下面这样更改它。

由此

before do
  allow_any_instance_of(SportRateManager)
    .to receive(:create)
    .and_return(true)
end
Run Code Online (Sandbox Code Playgroud)

对此:

let(:sport_manager) { instance_double(SportRateManager) }

before do
  allow(SportRateManager).to receive(:new).and_return(sport_manager)
  allow(sport_manager).to receive(:create).and_return(true)
end
Run Code Online (Sandbox Code Playgroud)

并具有完整的上下文:-之前

describe 'POST create' do
    let(:sport_rate) { build(:sport_rate) }
    let(:action) { post :create, sport_rate: sport_rate.attributes }

    context 'when sport rate manager created the rate successfully' do
      before do
        allow_any_instance_of(SportRateManager)
          .to receive(:create)
          .and_return(true)
      end

      it 'returns ok status' do
        action
        expect(response).to have_http_status(:ok)
      end
    end
Run Code Online (Sandbox Code Playgroud)

...-之后:

describe …
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails rubocop rubocop-rspec

6
推荐指数
1
解决办法
3814
查看次数

为什么“不要在`before`等钩子中使用`expect`”?

我的问题已经通过标题得到了很好的解释。我试图用谷歌搜索,但没有找到任何令人满意的东西。rubocop-rspec不允许expectinsidebefore,为什么呢?有充分的理由避免这种用法吗?感谢您提前的解释!

rspec rubocop-rspec

6
推荐指数
1
解决办法
2795
查看次数

RSpec + Rubocop - 为什么 receive_message_chain 是代码味道?

我即将为我的自定义验证器编写规范,它使用此链来检查使用 ActiveStorage 附加的文件是否为 txt:

return if blob.filename.extension.match?('txt')

通常,我可以通过这个调用来存根它:

allow(attached_file).to receive_message_chain(:blob, :byte_size) { file_size }

Rubocop 说这是一种冒犯,并向我指出了文档:https://www.rubydoc.info/gems/rubocop-rspec/1.7.0/RuboCop/Cop/RSpec/MessageChain

我必须为bloband声明 doublebyte_size并将它们存根在单独的行中,最终得到 5 行代码而不是 1 行。我在这里遗漏了什么吗?

rspec ruby-on-rails rubocop rubocop-rspec

5
推荐指数
1
解决办法
4225
查看次数

Rubocop:错误 - rubocop 返回退出代码 2

我刚刚将一个 Ruby on Rails 项目克隆到我的本地机器上以进行处理。我已运行该命令bundle install以安装项目所需的所有必要的 gem 和依赖项。

但是我的日志消息中不断弹出一条消息,这让我很担心。这是

错误:Rubocop 返回退出代码:2

我检查了rubocop我安装的版本,它似乎是最新的。我还对可能导致此错误的原因进行了一些研究,但我似乎还没有任何运气。我需要一些帮助。

ruby ruby-on-rails rubocop rubocop-rspec

5
推荐指数
1
解决办法
2536
查看次数

如何将 rubocop-rails 和 rubocop-rspec 添加到配置中?

> bundle exec rubocop
Run Code Online (Sandbox Code Playgroud)
The following RuboCop extension libraries are installed but not loaded in config:
  * rubocop-rails
  * rubocop-rspec
Run Code Online (Sandbox Code Playgroud)

我在哪里添加这些?

rspec ruby-on-rails rubocop rubocop-rspec

5
推荐指数
1
解决办法
1050
查看次数

Rubocop 错误:无法解析所需路径rails_helper

Rubocop 表示该线路require 'rails_helper'无法解析路径。

我通过 lsm-mode 调用的 Solargraph 使用 Rubocop,它位于使用 Rspec 处理 Ruby on Rails 项目的 Docker 容器内。Emacs 进程在本地笔记本电脑上运行,并使用 lsm-docker 激活 Solargraph。

docker compose 有三个容器,一个带有 Postgres 的 db 容器,一个运行 Rails 的 Web 容器,以及另一个用于 Solargraph 进程的容器。Solargraph 容器和 Web 容器从我正在构建的同一个图像开始。构建过程包括填充 Solargraph 的两个步骤:

( grep -s solargraph /hatred/Gemfile > /dev/null && /root/bin/solargraph download-core && /root/bin/solargraph bundle )
Run Code Online (Sandbox Code Playgroud)

我正在使用 Bundler 的 bin 目录中的 Solargraph 可执行文件。

我是 Rubocop、Solargraph 和 lsm-mode 的新手。

在我使用自动生成的规范文件中,rails g scaffold ...我有一个require 'rails_helper'. 做rspec path/to/spec它找到的工作 …

rubocop rubocop-rspec

3
推荐指数
1
解决办法
725
查看次数

标签 统计

rubocop-rspec ×6

rubocop ×5

rspec ×4

ruby-on-rails ×4

ruby ×2