This question already has an answer here:
这个问题已经有了答案:
- rspec how to run a single test? 12 answers
- rspec如何运行单个测试?12个答案
I've searched throughout the net but can't seem to find a solution to this and the following example is not working:
我搜索了整个网络,但似乎找不到解决办法,下面的例子不起作用:
# 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
any help guys?
任何帮助吗?
1 个解决方案
#1
3
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.
123是你的规范开始的行数。
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.
否则你就把1赋给了1。应该,这没有道理。
You also can't write "describe myText", because myText is not defined anywhere. You probably meant describe 'myText'
.
你也不能写“description myText”,因为myText在任何地方都没有定义。你可能指的是描述“我的文本”。
Finally, the preferred approach for RSpec assertions is
最后,RSpec断言的首选方法是
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
#1
3
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.
123是你的规范开始的行数。
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.
否则你就把1赋给了1。应该,这没有道理。
You also can't write "describe myText", because myText is not defined anywhere. You probably meant describe 'myText'
.
你也不能写“description myText”,因为myText在任何地方都没有定义。你可能指的是描述“我的文本”。
Finally, the preferred approach for RSpec assertions is
最后,RSpec断言的首选方法是
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