RSpec:未加载spec_helper.rb

时间:2017-10-18 13:32:58

标签: ruby selenium rspec

我正在学习测试自动化(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 --initdescribed

  • 它生成了文件“.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)

2 个答案:

答案 0 :(得分:1)

  

我总是从文件夹&#34; specs&#34;

执行我的specs.rb

唐&#39;吨

root 文件夹运行您的规范,即上一级。

你的命令应该是:

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

# (To run the full suite)
rspec

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

答案 1 :(得分:0)

它对我有用。我永远不会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
  

它生成了文件&#34; .rspec&#34;和&#34; specs \ spec_helper.rb&#34;

您确定您的规格目录是spec而不是specs吗?

修改

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

  

1.c)我总是从文件夹&#34; specs&#34;

执行我的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

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