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?
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.