I’m trying to do some things with rspec and I’m not able to access the variables I need in the places I need them. For example:
describe "some things" do
it "should have things in App" do
App.things.should_not be_nil #no problems here
# puts App.things.count
# => 10
end
# puts App.things.count
# => 0
App.things.each do |thing|
#this doesn't work at all as App.things is empty
it "#{thing.title} should have an account number" do
thing.acc_no.should_not be_nil
end
end
I guess this occurs because of the time the different blocks are called. Or maybe I’m missing the point completely.
I need to iterate over ‘things’ and make an assertion about each one, but I can’t do that because things doesn’t have any elements in the context of the describe block, only within the it block.
Any help?
It seems you’re doing it wrong: why test every single value of a collection?
create some fixtures and test that App has the correct number of created objects
use one of the created fixtures to test that has correct values, that runs validations, etc…
at least, you can use rspec’s shared examples to repeat common tests:
http://blog.davidchelimsky.net/2010/11/07/specifying-mixins-with-shared-example-groups-in-rspec-2/