ruby - Rspec:EmeraldComponent:Module 的未定义方法 `StandardError'

标签 ruby rspec tdd

在我的 gem 中,我有以下模块:

module EmeraldComponent

  def self.create(full_name)
    raise StandardError('Base directory for components is missing.') if base_directory_missing?
    raise StandardError('An Emerald Component must have a name.') if full_name.empty?
    raise StandardError('An Emerald Component must have a namespace.') if simple_name?(full_name)
    write_component(full_name)
    true
  end

  def self.write_component(full_name)
    ## To be implemented
  end

  def self.simple_name?(full_name)
    vet = full_name.split('.')
    vet.length == 1
  end

  def self.base_directory_missing?
    not (File.exist?(EmeraldComponent::BASE_DIRECTORY) && File.directory?(EmeraldComponent::BASE_DIRECTORY))
  end

end

在我对这个模块的 Rspec 测试中,我有这些:

context 'create' do

  it 'raises an error if the base directory for components is missing' do
    expect {
      EmeraldComponent.create('test.component.Name')
    }.to raise_error(StandardError)
  end

  it 'raises an error if it receives an empty string as component name' do
    expect {
      EmeraldComponent.create('')
    }.to raise_error(StandardError)
  end

  it 'raises an error if it receives a non-namespaced component name' do
    expect {
      EmeraldComponent.create('test')
    }.to raise_error(StandardError)
  end

  it 'returns true if it receives a non-empty and namespaced component name' do
    expect(EmeraldComponent.create('test.component.Name')).to be true
  end

碰巧当我运行测试时,除了第一个之外,所有的测试都通过了。这给了我以下错误。

1) EmeraldComponent Methods create returns true if it receives a non-empty and namespaced component name
Failure/Error: raise StandardError('Base directory for components is missing.') if base_directory_missing?

 NoMethodError:
   undefined method `StandardError' for EmeraldComponent:Module
 # ./lib/EmeraldComponent.rb:10:in `create'
 # ./spec/EmeraldComponent_spec.rb:48:in `block (4 levels) in <top (required)>'

如您所见,它表示 StandardError 未为 EmeraldComponent:Module 定义。

但是StandardError不属于EmeraldComponent:Module!

此外,同样的 StandardError 在其他测试中工作正常!

我已经和这个错误作斗争了一段时间,然后决定在这里发帖。有什么建议吗?

最佳答案

您应该在原地执行 StandardError.new 或在 create 方法中执行 StandardError

 def self.create(full_name)
    raise StandardError.new('Base directory for components is missing.') if base_directory_missing?
    raise StandardError.new('An Emerald Component must have a name.') if full_name.empty?
    raise StandardError.new('An Emerald Component must have a namespace.') if simple_name?(full_name)
    write_component(full_name)
    true
  end

关于ruby - Rspec:EmeraldComponent:Module 的未定义方法 `StandardError',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38799820/

相关文章:

ruby-on-rails - RSpec 3 : Simple validates_uniqueness_of Failure

ruby-on-rails - 我开始在 Rails 中进行测试,但我不确定我的测试

ruby-on-rails - 在 heroku 中是否可以返回应用程序的备份列表?

ruby-on-rails - 获取实例变量的名称

ruby - 我可以告诉或提示 RubyMine 局部变量或实例变量是什么类型吗?

ruby-on-rails - Rails gem 的 Rspec 测试

ruby - 为什么 Browsermob-Proxy-rb w/Selenium 无法任意记录 HTTP 流量?

c# - TDD 和模拟 TcpClient

ruby-on-rails - FactoryBot 对象缺少某些属性

ruby - |_| 是什么意思? ruby 中的意思?