ruby-on-rails - raise_error 规范在 Rspec 3.4 中不返回 true

标签 ruby-on-rails ruby rspec

我有以下类(class),我正在尝试为其编写规范:

module IntegrationError
  class Error < StandardError; end
  class BadRequest < IntegrationError::Error; end
  class LogicProblem < IntegrationError::Error; end

  def raise_logic_error!(message)
    raise IntegrationError::LogicProblem, message
  rescue => e
    Rails.logger.error e.message
    e.backtrace.each do |line|
      Rails.logger.error line if line.include?('integrations')
    end
  end

  def raise_bad_request!(message)
    raise IntegrationError::BadRequest, message
  end

  def log_bad_request!(message)
    Rails.logger.info message
  end
end

有规范

RSpec.describe 'IntegrationError', type: :integration do
  let!(:klass) { Class.new { include IntegrationError } }

  describe '#log_bad_request!' do
    it 'logs it' do
      expect(klass.new.log_bad_request!('TESTME')).to be_truthy
    end
  end

  describe '#raise_bad_request!' do
    it 'raises it' do
      binding.pry
      expect(klass.new.raise_bad_request!('TESTME')).to raise_error 
    end
  end
end

raise_bad_request 测试返回错误而不是 true。任何人都想过如何更好地将它写到它通过?

我正在使用 Rails 4 和 Rspec 3.4。

最佳答案

如果我没记错的话,我相信你在加注时需要通过一个障碍,像这样:

describe '#raise_bad_request!' do
  it 'raises it' do
    binding.pry
    expect{klass.new.raise_bad_request!('TESTME')}.to raise_error 
  end
end

参见 docs here

关于ruby-on-rails - raise_error 规范在 Rspec 3.4 中不返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38364697/

相关文章:

ruby-on-rails - 使用 rspec 进行 ActionMailer 测试

ruby-on-rails - 无法找到 new_post_path 和其他模板函数

ruby-on-rails - Rails 中的 NoMethodError::MailersController#preview

mysql - 创建具有多个字段键的模型关联

arrays - Ruby 迭代数组并在哈希中查找匹配项并替换数组中的元素

Java 静态 vs Ruby 的 self

ruby-on-rails - 测试页面重定向的问题

ruby-on-rails - 设计的database_authenticable的 "stretches"是什么意思?

ruby - 使用 Ruby 解析 HTML 表,Nokogiri 省略列标题

ruby-on-rails - 在 Rails/RSpec 请求测试中模拟非本地请求