我希望能够运行单个规范文件的测试—例如,对于我正在编辑的一个文件。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

当前回答

这个问题是一个老问题,但是当搜索如何运行单个测试时,它会出现在谷歌的顶部。我不知道这是否是最近添加的,但要运行一个单独的测试,你可以执行以下操作:

rspec path/to/spec:<line number>

其中-行号-是包含部分测试的行号。例如,如果你有这样的规格:

1: 
2: it "should be awesome" do
3:   foo = 3
4:   foo.should eq(3)
5: end
6:

假设它保存在spec/models/foo_spec.rb中。然后你会跑:

rspec spec/models/foo_spec.rb:2

它只会运行一个规格。事实上,这个数字可以是2到5之间的任何数字。

其他回答

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

从帮助(spec -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

虽然这个问题有很多很好的答案,但没有一个使用Rspec标记方法。

我使用标记在不同的文件中运行一个或多个规范——只运行与当前开发任务相关的规范。

例如,我添加了标签“dev”,值为“current”:

it "creates an user", dev: :current do
  user = create(:user)
  expect(user.persisted?).to be_truthy
end

然后我就跑

bundle exec rspec . --tag dev:current

可以在单个规格或组中设置不同的标签/值。

这个问题是一个老问题,但是当搜索如何运行单个测试时,它会出现在谷歌的顶部。我不知道这是否是最近添加的,但要运行一个单独的测试,你可以执行以下操作:

rspec path/to/spec:<line number>

其中-行号-是包含部分测试的行号。例如,如果你有这样的规格:

1: 
2: it "should be awesome" do
3:   foo = 3
4:   foo.should eq(3)
5: end
6:

假设它保存在spec/models/foo_spec.rb中。然后你会跑:

rspec spec/models/foo_spec.rb:2

它只会运行一个规格。事实上,这个数字可以是2到5之间的任何数字。

如果您将rspec作为插件而不是gem安装,那么您将无法获得规范可执行文件。

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

eg:

ruby myclass_spec.rb