# How to load all models when testing?

**URL:** <https://www.ruby-forum.com/t/how-to-load-all-models-when-testing/76954>\
**Category:** Rails\
**Created:** [November 22, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/how-to-load-all-models-when-testing/76954 "2006-11-22T20:47:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [November 22, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/how-to-load-all-models-when-testing/76954/1 "2006-11-22T20:47:34Z")

</div>

I do something like the following in my models:

class NoDeleteCompanyError \< RuntimeError; end

class Company \< ActiveRecord::Base  
# model code  
end

When I try to do:

assert\_rais(NoDeleteCompanyError) { some\_block }

It says that NoDeleteCompanyError is an uninitialized constant.

To fix this I just do fixtures :companies.

Is there anyway to default load all models when testing so I dont have  
to do this?

Thanks.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [November 22, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/how-to-load-all-models-when-testing/76954/2 "2006-11-22T21:15:41Z")

</div>

> assert\_rais(NoDeleteCompanyError) { some\_block }
> 
> It says that NoDeleteCompanyError is an uninitialized constant.
> 
> To fix this I just do fixtures :companies.
> 
> Is there anyway to default load all models when testing so I dont have  
> to do this?

Probably 😉 But another way would be to put your NoDeleteCompanyError  
into no\_delete\_company\_error.rb in your models directory…

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [November 23, 2006, 12:53am UTC](https://www.ruby-forum.com/t/how-to-load-all-models-when-testing/76954/3 "2006-11-23T00:53:16Z")

</div>

Philip H. wrote:

> > assert\_rais(NoDeleteCompanyError) { some\_block }
> > 
> > It says that NoDeleteCompanyError is an uninitialized constant.
> > 
> > To fix this I just do fixtures :companies.
> > 
> > Is there anyway to default load all models when testing so I dont have  
> > to do this?
> 
> Probably 😉 But another way would be to put your NoDeleteCompanyError  
> into no\_delete\_company\_error.rb in your models directory…

Or it will probably work if you put the exception class inside your  
model

class Model  
class MyException \< RuntimeError; end  
end

Also, check out the all\_fixtures pluign I wrote:  
[http://beautifulpixel.com/all\_fixtures/index.html](http://beautifulpixel.com/all_fixtures/index.html)

It makes fixture management quite painless.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [November 23, 2006, 7:51am UTC](https://www.ruby-forum.com/t/how-to-load-all-models-when-testing/76954/4 "2006-11-23T07:51:47Z")

</div>

Ben J. wrote:

> The problem I have with that is that I cant catch that specific class  
> outside of that model because it gives me an unititalized constant  
> error. That’s why I put it outside the class.

Try it inside the class, and use Company::NoDeleteCompanyError to  
identify the Exception class.

assert\_raise(Company::NoDeleteCompanyError) do  
some\_block  
end

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [November 23, 2006, 6:20am UTC](https://www.ruby-forum.com/t/how-to-load-all-models-when-testing/76954/5 "2006-11-23T06:20:15Z")

</div>

Alex W. wrote:

> Philip H. wrote:
> 
> > > assert\_rais(NoDeleteCompanyError) { some\_block }
> > > 
> > > It says that NoDeleteCompanyError is an uninitialized constant.
> > > 
> > > To fix this I just do fixtures :companies.
> > > 
> > > Is there anyway to default load all models when testing so I dont have  
> > > to do this?
> > 
> > Probably 😉 But another way would be to put your NoDeleteCompanyError  
> > into no\_delete\_company\_error.rb in your models directory…
> 
> Or it will probably work if you put the exception class inside your  
> model
> 
> class Model  
> class MyException \< RuntimeError; end  
> end
> 
> Also, check out the all\_fixtures pluign I wrote:  
> [http://beautifulpixel.com/all\_fixtures/index.html](http://beautifulpixel.com/all_fixtures/index.html)
> 
> It makes fixture management quite painless.

The problem I have with that is that I cant catch that specific class  
outside of that model because it gives me an unititalized constant  
error. That’s why I put it outside the class.
