当我必须在RSPEC中更改方法时,如何检测时间?

发布于 2025-01-30 07:25:16 字数 902 浏览 1 评论 0 原文

假设我有以下代码。

class Answer
  enum type: %i[text checkbox image]

  def round_type
    case answer.type
    when text, checkbox
      :text
    when image
      :multimedia
    else
      raise 'Unknown type'
    end
  end  
end
require 'rails_helper'

RSpec.describe Answer, type: :model do
  describe '#round_type' do
    context 'when type is text' do
      it 'returns text' do
        # omitted 
      end
    end
    context 'when type is checkbox' do
      it 'returns text' do
      end
    end
    context 'when type is image' do
      it 'returns multimedia' do
      end
    end    
  end
end

然后,我将视频类型添加到枚举中。我希望当类型为视频时,方法会返回多媒体。

但是Round_Type方法和测试代码不支持视频类型。因此,当我遇到生产错误时,我最终会意识到这一点。

我想知道在发生错误之前必须更改该方法。

因此,这是我的问题:当我必须在RSPEC中更改方法时,该如何检测时间?

Suppose I have the following code.

class Answer
  enum type: %i[text checkbox image]

  def round_type
    case answer.type
    when text, checkbox
      :text
    when image
      :multimedia
    else
      raise 'Unknown type'
    end
  end  
end
require 'rails_helper'

RSpec.describe Answer, type: :model do
  describe '#round_type' do
    context 'when type is text' do
      it 'returns text' do
        # omitted 
      end
    end
    context 'when type is checkbox' do
      it 'returns text' do
      end
    end
    context 'when type is image' do
      it 'returns multimedia' do
      end
    end    
  end
end

Then I add video type to the enum. And I expect the method returns multimedia when the type is video.

But the round_type method and the test codes are not support video type. So I will finally realize it when I get an error in production.

I'd like to know what I have to change the method before the error occurs.

So, this is my question: How can I detect the timing when I have to change a method in rspec?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜还是长夜 2025-02-06 07:25:16

如果我正确理解您,则必须使您的规格更具动态性,并且还必须测试 else 语句:

class Answer < ApplicationRecord
  enum type: %i[text checkbox image]

  def round_type
    case type
    when 'text', 'checkbox'
      :text
    when 'image'
      :multimedia
    else
      raise 'Unknown type'
    end
  end  
end
RSpec.describe Answer, type: :model do
  describe '#round_type' do
    it 'raises error for unknown type' do
      # empty `type` is an unknown type in this situation
      expect { Answer.new.round_type }.to raise_error
    end

    it 'does not raise error for available types' do
      # NOTE: loop through all types and check that `round_type` method
      #       recognizes each one.
      Answer.types.each_key do |key|
        expect { Answer.new(type: key).round_type }.to_not raise_error
      end
    end
  end
end

下次您添加新的 type ,然后忘记要更新 round_type 方法,最后一个规格将失败。

If I understood you correctly, you have to make your specs a bit more dynamic and you have to test the else statement as well:

class Answer < ApplicationRecord
  enum type: %i[text checkbox image]

  def round_type
    case type
    when 'text', 'checkbox'
      :text
    when 'image'
      :multimedia
    else
      raise 'Unknown type'
    end
  end  
end
RSpec.describe Answer, type: :model do
  describe '#round_type' do
    it 'raises error for unknown type' do
      # empty `type` is an unknown type in this situation
      expect { Answer.new.round_type }.to raise_error
    end

    it 'does not raise error for available types' do
      # NOTE: loop through all types and check that `round_type` method
      #       recognizes each one.
      Answer.types.each_key do |key|
        expect { Answer.new(type: key).round_type }.to_not raise_error
      end
    end
  end
end

Next time you add a new type and forget to update round_type method, the last spec will fail.

https://relishapp.com/rspec/rspec-expectations/v/3-11/docs/built-in-matchers/raise-error-matcher

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文