只测试一个或用Rspec描述

时间:2011-06-29 09:27:40

标签: ruby-on-rails-3 rspec rspec2

在TestUnit上,您可以使用-n选项

在文件中启动一个测试

例如

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  test "the truth" do
    assert true
  end

  test "the truth 2" do
    assert true
  end

end

你只能执行测试真相

ruby -Itest test/unit/user_test.rb -n test_the_truth

输出

1 tests, 1 assertions, 0 failures, 0 errors, 0 skip

如何使用rspec?

该命令似乎不起作用

rspec spec/models/user_spec.rb -e "User the truth"

3 个答案:

答案 0 :(得分:7)

您没有包含规范的来源,因此很难说问题出在哪里,但通常您可以使用-e选项来运行单个示例。鉴于此规范:

# spec/models/user_spec.rb
require 'spec_helper'
describe User do

  it "is true" do
    true.should be_true
  end

  describe "validation" do
    it "is also true" do
      true.should be_true
    end
  end

end

此命令行:

rspec spec/models/user_spec.rb -e "User is true"

将产生此输出:

Run filtered including {:full_description=>/(?-mix:User\ is\ true)/}
.

Finished in 0.2088 seconds
1 example, 0 failures

如果你想调用另一个例子,那个嵌套在验证组中的例子,你可以使用它:

rspec spec/models/user_spec.rb -e "User validation is also true"

或者运行验证组中的所有示例:

rspec spec/models/user_spec.rb -e "User validation"

答案 1 :(得分:2)

您还可以选择要在哪个行中执行的测试用例。

rspec spec/models/user_spec.rb:8

通过在测试用例范围内传递任何行,只会执行此测试用例。您也可以使用它来执行测试中的整个上下文。

答案 2 :(得分:1)

至少在Rspec 2.11.1中,您可以使用以下所有选项:

  

**过滤/标记**

In addition to the following options for selecting specific files, groups,
or examples, you can select a single example by appending the line number to
the filename:

  rspec path/to/a_spec.rb:37

-P, --pattern PATTERN            Load files matching pattern (default: "spec/**/*_spec.rb").
-e, --example STRING             Run examples whose full nested names include STRING (may be
                                   used more than once)
-l, --line_number LINE           Specify line number of an example or group (may be
                                   used more than once).
-t, --tag TAG[:VALUE]            Run examples with the specified tag, or exclude examples
                                 by adding ~ before the tag.
                                   - e.g. ~slow
                                   - TAG is always converted to a symbol