如何在rspec中运行特定的单个测试?

时间:2015-07-31 19:11:14

标签: ruby-on-rails rspec

我已经在整个网络中进行了搜索,但似乎无法找到解决方案,以下示例无效:

# spec/my_spec.rb
describe myText do
  it "won't work" do
    raise "never reached"
  end

  it "will work", :focus => true do
    1.should = 1
  end
end

$ rspec --tag focus spec/my_spec.rb

任何帮助人员?

1 个答案:

答案 0 :(得分:4)

In general, to execute a single spec, you can just do

rspec path/to/your/spec.rb:123

where 123 is the number of the line that your spec starts at.

However, your specific example can never work because you have two typos:

1.should = 1

should be

1.should eq 1      # deprecated to use 'should'

because otherwise you're assigning "1" to "1.should", which doesn't make sense.

You also can't write "describe myText", because myText is not defined anywhere. You probably meant describe 'myText'.

Finally, the preferred approach for RSpec assertions is

expect(1).to eq 1  # preferred

To prove this works, I did:

mkdir /tmp/example
gem_home .
gem install rspec

cat spec.rb
# spec.rb
describe "example" do
  it "won't work" do
    raise "never reached"
  end

  it "will work", :focus => true do
    expect(1).to eq 1
  end
end

and this passes, executing only the "will work" spec:

rspec --tag focus spec.rb
Run options: include {:focus=>true}
.

Finished in 0.0005 seconds (files took 0.05979 seconds to load)
1 example, 0 failures