【问题标题】:RSpec: The spec_helper.rb is not loadedRSpec:未加载 spec_helper.rb
【发布时间】:2017-10-18 13:32:57
【问题描述】:

我正在学习测试自动化(Ruby、selenium-webdriver、RSpec)。但是我遇到了一个问题,无法自己解决。

spec_helper.rb (我认为)没有按照here 的描述加载。

1.) 起初我只是将“spec_helper.rb”放入文件夹“specs”

1.b) 我插入我的“spec1.rb”

    require 'spec_helper'

1.c) 我总是从文件夹“specs”中执行我的 specs.rb

    rspec spec1.rb 
  • 这命中了这个错误,尽管文件在这个地方:

    Failure/Error: return gem_original_require(path)
    
    LoadError:
     cannot load such file -- spec_helper
    # ./spec2.rb:1:in `<top (required)>'
    No examples found. 
    

2.) 然后我在项目文件夹中运行rspec --init,作为described

  • 它生成了文件“.rspec”和“specs\spec_helper.rb”
  • 在 .rspec 文件中写入默认值:

    --require spec_helper
    

2.b) 然后我在文件“specs\spec_helper.rb”中写道:puts "I am loaded"

  • 这是我的“spec\spec_helper.rb”代码:

    # This file was generated by the `rspec --init` command. Conventionally, all
    # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
    # The generated `.rspec` file contains `--require spec_helper` which will cause
    # this file to always be loaded, without a need to explicitly require it in any
    # files.
    
    puts "I am loaded"
    
    require 'selenium-webdriver'
    require 'rspec/expectations'
    
  • 但是运行rspec spec.rb没有任何结果

  • 我的specs.rb 位于正确的文件夹中
  • (没有spec_helper.rb 我的测试运行良好。)

3.) 最后我再次尝试插入“specs\spec.rb”

    require 'spec_helper'
  • 那么它肯定又遇到了错误:

    Failure/Error: return gem_original_require(path)
    
    LoadError:
     cannot load such file -- spec_helper
    # ./spec2.rb:1:in `<top (required)>'
    No examples found.
    

我在Win10上安装成功:

ruby 2.3.3

LOCAL GEMS:
bigdecimal (1.2.8)
childprocess (0.8.0)
did_you_mean (1.0.0)
diff-lcs (1.3)
ffi (1.9.18 x64-mingw32)
io-console (0.4.5)
json (2.1.0, 1.8.3)
minitest (5.8.5)
net-telnet (0.1.1)
power_assert (0.2.6)
psych (2.1.0)
rake (10.4.2)
rdoc (4.2.1)
rspec (3.6.0)
rspec-core (3.6.0)
rspec-expectations (3.6.0)
rspec-mocks (3.6.0)
rspec-support (3.6.0)
rubyzip (1.2.1)
selenium-webdriver (3.6.0)
test-unit (3.1.5)

【问题讨论】:

  • 您需要将require 'spec_helper' 放在每个规范的顶部。
  • 它不会被魔法加载!
  • 请用这种格式做点什么...

标签: ruby selenium rspec


【解决方案1】:

我总是从文件夹“specs”中执行我的specs.rb

不要。

root 文件夹(即上一层)运行您的规范。

你的命令应该是:

# (To run just the one file)
rspec specs\spec.rb

# (To run the full suite)
rspec

旁注:不要通过将文件命名为spec1.rb 来违反约定。所有测试文件应命名为:&lt;something&gt;_spec.rb;任何其他文件都应用于辅助方法等。

【讨论】:

    【解决方案2】:

    它对我有用。我从不require 'spec_helper',因为它是由.rspec 文件自动完成的。我关注了Effective Testing with RSpec 3,按照你的描述做了rspec --init

    文件.../spec/spec_helper.rb

    puts "this is #{File.basename(__FILE__)}"
    # This file was generated by the `rspec --init` command.
    ...
    

    测试文件:

    # file .../spec/t2_spec.rb
    
    class XXX
    end
    
    RSpec.describe XXX do
        describe '#xxx' do
            it 'xxx' do
                puts '... testing ...'
            end
        end
    end
    

    执行(OS X 上的 RSpec 3.6.0.beta2):

    $ rspec
    this is spec_helper.rb
    
    XXX
      #xxx
    ... testing ...
        xxx
    
    Finished in 0.001 seconds (files took 0.10816 seconds to load)
    1 example, 0 failures
    

    它生成了文件“.rspec”和“specs\spec_helper.rb”

    你确定你的 specs 目录是 spec 而不是 specs 吗?

    编辑

    我再次阅读了您的帖子并看到:

    1.c) 我总是从文件夹“specs”中执行我的 specs.rb

    并检查它是否有所作为。确实如此:

    $ cd spec/
    $ ls
    examples.txt   spec_helper.rb t2_spec.rb     t_specxxx.rb
    $ rspec t2_spec.rb 
    ... testing ...
    .
    
    Finished in 0.00108 seconds (files took 0.10637 seconds to load)
    1 example, 0 failures
    

    可以看到spec_helper.rb没有被处理:

    • 消息未显示
    • 单点而不是缩进的文档,表示这段代码

    .

      if config.files_to_run.one?
        # Use the documentation formatter for detailed output,
        # unless a formatter has already been configured
        # (e.g. via a command-line flag).
        config.default_formatter = "doc"
      end
    

    尚未执行。正如汤姆所说,从libspec 的父级运行rspec

    【讨论】:

    • @Webdriver 已编辑,不要从spec 运行。
    • 非常感谢您的意见。 @Tom Lord @BernardK。我确实从spec 运行rspec
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 2023-03-05
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多