【问题标题】:Why does FakeFS break RSpec?为什么 FakeFS 会破坏 RSpec?
【发布时间】:2016-10-08 01:01:41
【问题描述】:

test_spec.rb:(来自 FakeFS example

require 'fakefs/spec_helpers' 

describe 'Test' do
  include FakeFS::SpecHelpers
  it 'should fail' do
    expect(1).to eq(2)
  end
end

describe 'Test2' do
  it 'should fail' do
    expect(1).to eq(2)
  end
end

rspec spec/test_spec.rb 在第一次测试中返回superclass mismatch for class File,在第二种情况下返回正常expected: 2 got: 1。匹配器更改(例如be_kind_of(String))不会影响结果。为什么会发生这种情况,如何解决?

ruby -v

ruby 2.4.0dev (2016-03-19 trunk 54188) [x86_64-linux]

【问题讨论】:

标签: ruby rspec


【解决方案1】:

我刚遇到这个问题,接受的答案对我没有帮助。

但我最终通过在我的spec_helper.rb 顶部添加以下行来解决这个问题:

require 'pp'

我有一个带有以下行的 .rspec 文件,以确保始终加载 spec_helper:

--require spec_helper

在 FakeFS 自述文件中记录了您需要在 fakefs 之前要求 pp 以避免此问题,但我自己并没有要求 pp。它一定是我使用的其他 gem 隐式需要的*。

因此,通过在 fakefs 之前明确要求 pp,我的规范现在可以正常运行。

* 我怀疑 RSpec 使用 pp 来打印漂亮的错误消息,因为我可能会导致 expect(true).to eq false 行中的异常

【讨论】:

    【解决方案2】:

    感谢@d.g link 到 fakefs 问题。有效的东西:

    宝石文件

    gem 'fakefs', require: 'fakefs/safe'
    

    spec/spec_helper.rb

    require 'fakefs/spec_helpers'
    
    RSpec.configure do |config|
      config.include FakeFS::SpecHelpers, fakefs: true
    end
    

    test_spec.rb

    require_relative 'spec_helper.rb'
    
    describe 'Test', fakefs: true do
      it 'should fail' do
        expect(1).to be_kind_of(String)
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多