RSpec 报告与性能
我希望能够编写一个规范,例如
describe Foo do
before :each do
@hash = some_very_expensive_setup_op
end
describe "hash" do
subject{@hash}
its([:a]){should == 10}
its([:b]){should == 20}
its([:c]){should == 30}
end
end
RSpec 的工作方式(相当合理)是在每个 its 块之前执行 before 块。在许多情况下,这就是您想要的,但在上述情况和我的许多测试中,最后的叶子其块正在做出没有副作用的断言。
我可以将规范重写为
describe Foo do
before :each do
@hash = some_very_expensive_setup_op
end
describe "hash" do
it "should have some attributes" do
@hash[:a].should == 10
@hash[:b].should == 20
@hash[:c].should == 30
end
end
end
现在所有断言都在单个块中进行。该规范在功能上是相同的,但我没有得到第一个版本的有趣报告,其中列出了文档格式化程序中的每个断言。
输出对我来说很重要,因为我尝试将输出用作 Web api 使用者的文档。例如,对于我的一项规格,我有一个类似的输出
GET /status.json?ago=:ago
it should behave like authenticated
GET /status.json
accepts a valid user
rejects an invalid user
request
request attributes
:ago - number of seconds of history to calculate statistics
:current_user ( implicit )
response attributes
scale
downtime
points
nextlevel
,但随着属性数量的增加,它会减慢规格。
有没有办法解决细粒度报告之间的这种紧张关系? 输出和测试性能?
I'd like to be able to write a spec such as
describe Foo do
before :each do
@hash = some_very_expensive_setup_op
end
describe "hash" do
subject{@hash}
its([:a]){should == 10}
its([:b]){should == 20}
its([:c]){should == 30}
end
end
And the way RSpec works, quite reasonably, is to execute the before block before each its block. In many cases this is what you want but in the above case and in many of my tests the final leaf its blocks are making assertions that have no side effects.
I could rewrite the spec as
describe Foo do
before :each do
@hash = some_very_expensive_setup_op
end
describe "hash" do
it "should have some attributes" do
@hash[:a].should == 10
@hash[:b].should == 20
@hash[:c].should == 30
end
end
end
Now all the assertions are made within a single block. The spec is functionally identical but I don't get the juicy reporting of the first version listing each assertion in the documentation formatter.
The output is important to me because I try to use the output as documentation for consumers of the web api. For example for one of my specs I have an output like
GET /status.json?ago=:ago
it should behave like authenticated
GET /status.json
accepts a valid user
rejects an invalid user
request
request attributes
:ago - number of seconds of history to calculate statistics
:current_user ( implicit )
response attributes
scale
downtime
points
nextlevel
But as the number of attributes rises it slows down the specs.
Are there any solutions to this tension between fine grained reporting
output and test performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
before :all
,它将在描述中的所有it
块之前运行给定块一次。如果您正在使用事务功能(Rails 中的默认功能),则需要小心一些。在
before :all
中进行的插入不会回滚,并且您还应该.reload
您创建的任何模型,以防它们被测试修改。(为了完整起见,在 :suite 之前还有一个
)
文档:
https://www .relishapp.com/rspec/rspec-core/v/2-11/docs/hooks/before-and-after-hooks
You can use
before :all
, which will run a given block once before allit
blocks in the describe.If you are using transactional features (as is default in Rails), you need to be a little careful. Inserts made in a
before :all
will not be rolled back, and you should also.reload
any models you create in case they are modified by tests.(For completeness, there is also a
before :suite
)Documentation:
https://www.relishapp.com/rspec/rspec-core/v/2-11/docs/hooks/before-and-after-hooks
解决方案是使用类似 https://github.com/LRDesign/rspec-steps
状态的 包在调用之间进行维护,如果您愿意,您可以测试一系列步骤。
The solution is to use packages like https://github.com/LRDesign/rspec-steps
State is maintained between it calls you can test a sequence of steps if you like.