如何在RSpec中运行单个测试/规格文件?

时间:2020-03-06 14:49:36  来源:igfitidea点击:

例如,我希望能够对我正在编辑的一个文件运行单个规格文件的测试。 rake spec执行所有的规范。我的项目不是Rails项目,因此rake spec:doc不起作用。

不知道这是否重要,但这是我的目录结构。

./Rakefile
./lib
./lib/cushion.rb
./lib/cushion
./lib/cushion/doc.rb
./lib/cushion/db.rb
./spec
./spec/spec.opts
./spec/spec_helper.rb
./spec/db_spec.rb

解决方案

原始调用:

rake spec SPEC=spec/controllers/sessions_controller_spec.rb \
          SPEC_OPTS="-e \"should log in with cookie\""

现在找出如何将其嵌入到编辑器中。

或者,我们可以跳过rake并使用" rspec"命令:

rspec path/to/spec/file.rb

就我们而言,我认为只要./spec/db_spec.rb文件包含适当的帮助程序,它就可以正常工作。

如果我们使用的是较旧的rspec版本,则为:

spec path/to/spec/file.rb

如果将rspec作为插件而不是gem安装,则将没有spec可执行文件。

无论如何,我们需要做的就是使用ruby运行文件。 rspec代码足够聪明,可以为我们运行测试。

例如:

ruby myclass_spec.rb

另外,看看自动测试。

在命令窗口中运行自动测试意味着我们保存该规范文件时将执行该文件。此外,只要运行我们要指定的文件,它就会运行。

例如,如果我们有一个名为person_spec.rb的模型规格文件,以及一个名为person.rb的模型文件,那么无论何时从编辑器中保存这些文件之一,都会执行该规格文件。

斑点病毒

http://github.com/grosser/single_test允许我们执行类似的操作。

rake spec:user          #run spec/model/user_spec.rb (searches for user*_spec.rb)
rake test:users_c       #run test/functional/users_controller_test.rb
rake spec:user:token    #run the first spec in user_spec.rb that matches /token/
rake test:user:token    #run all tests in user_test.rb that match /token/
rake test:last
rake spec:last

来自帮助(规格-h):

-l, --line LINE_NUMBER           Execute example group or example at given line.
                                 (does not work for dynamically generated examples)

示例:spec spec / runner_spec.rb -l 162