【发布时间】: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'放在每个规范的顶部。 -
它不会被魔法加载!
-
请用这种格式做点什么...