Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: Unit Testing non activerecord models and still load fixtures

I may be missing something but I am stuck in this scenario:

I have a non activerecord model, which I want to test. I have derived its test case class from: Test::Unit::TestCase.

However, the test case class for the model, uses within itself, other activerecord model classes and I want to load fixtures for them. My problem is that the fixtures class method is available only when I subclass the test case class from ActiveSupport::TestCase (it is defined within ActiveRecord::TestFixtures which gets included in ActiveSupport::TestCase).

Any help, coz running the tests gives me the error: undefined method "fixtures" (which is understandable) and in case I derive my test case class from ActiveSupport::TestCase it complains that there is no corresponding DB table. Also, I don't want to create a dummy table for backing my model class.

Thanks a ton!

like image 918
Vaibhav Gumashta Avatar asked Feb 06 '11 07:02

Vaibhav Gumashta


People also ask

What type of test is not typical in a Rails application?

Integration. Don't try to test all the combinations in integration tests. That's what unit tests are for. Just test happy-paths or most common cases.

What exactly are harnesses and fixtures in the Ruby?

14) What exactly are Harnesses and Fixtures in the Ruby? These are basically the supporting codes with the help of which the users can easily write and can run the test cases. With the help of a rake, the users can then simply proceed with the automated tests.

How do Fixtures work rails?

Fixtures are data that you can feed into your unit testing. They are automatically created whenever rails generates the corresponding tests for your controllers and models. They are only used for your tests and cannot actually be accessed when running the application.

What are the instance variables available in functional testing?

You also have access to three instance variables in your functional tests: @controller – The controller processing the request. @request – The request. @response – The response.


1 Answers

You can use ActiveSupport::TestCase without the class being an ActiveRecord class (an example - but this project doesn't use fixtures), so something else must be causing this. It must be something about how you are using fixtures, or your fixture configuration. Perhaps, by chance, do you have a fixture file for this non-AR model you are trying to test?

like image 117
wuputah Avatar answered Oct 05 '22 23:10

wuputah