# Fixtures not working in functional tests when run through Ra

**URL:** <https://www.ruby-forum.com/t/fixtures-not-working-in-functional-tests-when-run-through-ra/51935>\
**Category:** Rails\
**Created:** [December 4, 2005, 1:13am UTC](https://www.ruby-forum.com/t/fixtures-not-working-in-functional-tests-when-run-through-ra/51935 "2005-12-04T01:13:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 4, 2005, 1:13am UTC](https://www.ruby-forum.com/t/fixtures-not-working-in-functional-tests-when-run-through-ra/51935/1 "2005-12-04T01:13:55Z")

</div>

I’ve got some functional tests that load up fixtures…and they run  
just fine when I run them through the Ruby interpretter. But if I run  
them with Rake, it says it can’t find any of the records and causes a  
bunch of errrors. No idea why this is happening…does Rake do stuff  
differently than just running the individual Ruby file? Thanks for  
any help.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 4, 2005, 1:31pm UTC](https://www.ruby-forum.com/t/fixtures-not-working-in-functional-tests-when-run-through-ra/51935/2 "2005-12-04T13:31:00Z")

</div>

Pat M. wrote:

> I’ve got some functional tests that load up fixtures…and they run  
> just fine when I run them through the Ruby interpretter. But if I run  
> them with Rake, it says it can’t find any of the records and causes a  
> bunch of errrors. No idea why this is happening…does Rake do stuff  
> differently than just running the individual Ruby file? Thanks for  
> any help.
> 
> Pat

Pat,

Can you send the first several errors you’re getting, along with the  
command  
that Rake is running?

You can try running the same command that rake does from the command  
line. I  
typically do that, but only list several test \*.rb files at once to see  
what’s  
going on.

The other thing is to tail the log/test.log.

Are you using foreign keys in your schema? I am and when I ran into  
these  
problems, I didn’t have the fixtures defined correctly for each of my  
models or  
controllers and didn’t clean up the fixtures. I ended up putting this  
into my  
test/test\_helper.rb to clean up fixtures:

# Delete the fixtures associated with this test suite in the reverse

# order that they are loaded. Take care to use the database

# connection associated with the fixture, not with the model being

# tested.

def teardown  
self.class.fixture\_table\_names.reverse.each do |table\_name|  
klass\_name = Inflector.classify(table\_name.to\_s)  
if Object.const\_defined?(klass\_name)  
klass = Object.const\_get(klass\_name)  
klass.connection.delete(“DELETE FROM #{table\_name}”, ‘Fixture  
Delete’)  
else  
flunk(“Cannot find class for table ‘#{table\_name}’ to delete  
fixtures”)  
end  
end  
end

Rake will run either the clone\_structure\_to\_test or the  
clone\_schema\_to\_test  
task before running the test.

Regards,  
Blair

–  
Blair Z., Ph.D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subversion and Orca training and consulting  
[http://www.orcaware.com/svn/](http://www.orcaware.com/svn/)

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 4, 2005, 1:31pm UTC](https://www.ruby-forum.com/t/fixtures-not-working-in-functional-tests-when-run-through-ra/51935/3 "2005-12-04T13:31:02Z")

</div>

On 12/3/05, Blair Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Pat,  
> Are you using foreign keys in your schema? I am and when I ran into these  
> klass\_name = Inflector.classify(table\_name.to\_s)  
> task before running the test.
> 
> Regards,  
> Blair

I forgot to mention, I’ve been using your teardown method to clear the  
tables after each test.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 4, 2005, 1:39pm UTC](https://www.ruby-forum.com/t/fixtures-not-working-in-functional-tests-when-run-through-ra/51935/4 "2005-12-04T13:39:14Z")

</div>

Pat M. wrote:

> On 12/3/05, Blair Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> Yes I’m using foreign keys, but all the unit tests run just fine.  
> It’s only the functional tests that have a problem with the fixtures  
> apparently, and only when I run rake:

Just to be dumb about this, but you have the ‘fixture’ lines in the  
functional  
tests to load the data?

If you load script/console and try to find the user id 1003, do you find  
it?

> $ rake test\_functional  
> …
> 
> 1. Error:  
> test\_create\_without\_name(Controllers::Admin::CategoryControllerTest):

What does test\_create\_without\_name look like?

> `setup\_without\_fixtures’  
> @response = ActionController::TestResponse.new
> 
> ```
> # Log the user in
> @request.session[:user] = users(:firstuser)
> 
> ```

What does users(:firstuser) do?

So if you type

ruby test/functional/admin/category\_controller\_test.rb

the test runs?

Regards,  
Blair

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 4, 2005, 1:35pm UTC](https://www.ruby-forum.com/t/fixtures-not-working-in-functional-tests-when-run-through-ra/51935/5 "2005-12-04T13:35:10Z")

</div>

On 12/3/05, Blair Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Pat,  
> Are you using foreign keys in your schema? I am and when I ran into these  
> klass\_name = Inflector.classify(table\_name.to\_s)  
> task before running the test.
> 
> Regards,  
> Blair

Yes I’m using foreign keys, but all the unit tests run just fine.  
It’s only the functional tests that have a problem with the fixtures  
apparently, and only when I run rake:

$ rake test\_functional  
…

1. Error:  
test\_create\_without\_name(Controllers::Admin::CategoryControllerTest):  
ActiveRecord::RecordNotFound: Couldn’t find User with ID=1003  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active\_record/base.rb:428:in  
`find' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:390:in `find’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active\_record/fixtures.rb:456:in  
`users' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:452:in `users’  
./test/functional/admin/category\_controller\_test.rb:16:in  
`setup_without_fixtures' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:522:in `setup’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active\_record/fixtures.rb:520:in  
`setup’

That’s repeated for every test I run. In the test, I’m trying to open  
the fixture in the setup method:  
def setup  
@controller = TicketController.new  
@request = ActionController::TestRequest.new  
@response = ActionController::TestResponse.new

```
# Log the user in
@request.session[:user] = users(:firstuser)

```

end

Line 16 is the @request.session… line, the error referencing  
setup\_without\_fixtures.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 4, 2005, 3:00pm UTC](https://www.ruby-forum.com/t/fixtures-not-working-in-functional-tests-when-run-through-ra/51935/6 "2005-12-04T15:00:53Z")

</div>

On 12/3/05, Blair Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Pat M. wrote:
> 
> > On 12/3/05, Blair Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > Yes I’m using foreign keys, but all the unit tests run just fine.  
> > It’s only the functional tests that have a problem with the fixtures  
> > apparently, and only when I run rake:
> 
> Just to be dumb about this, but you have the ‘fixture’ lines in the functional  
> tests to load the data?

fixtures :categories, :users, :tickets, :messages

> If you load script/console and try to find the user id 1003, do you find it?

Yes

> > $ rake test\_functional  
> > …
> > 
> > 1. Error:  
> > test\_create\_without\_name(Controllers::Admin::CategoryControllerTest):
> 
> What does test\_create\_without\_name look like?

def test\_create\_without\_name  
num\_categories = Category.count  
post :create, :category =\> { }  
assert\_response :success  
assert\_equal num\_categories, Category.count  
end

> > `setup\_without\_fixtures’  
> > @response = ActionController::TestResponse.new
> > 
> > ```
> > # Log the user in
> > @request.session[:user] = users(:firstuser)
> > 
> > ```
> 
> What does users(:firstuser) do?

I commented out all the tests and just did a test\_truth method, and  
puts users(:firstuser).login in the setup method. It output the login  
fine. So now I’m really confused as to what’s wrong.

> So if you type
> 
> ruby test/functional/admin/category\_controller\_test.rb
> 
> the test runs?

Yes

Another thing that’s confusing me is that if I select \* from users  
after I run the functional test, I get all the records that are  
defined in the fixtures file. I don’t think I should though, because  
they should all be deleted in the teardown method, right? I know the  
teardown method is being called because I’ve put some puts statements  
in there to make sure.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 4, 2005, 3:04pm UTC](https://www.ruby-forum.com/t/fixtures-not-working-in-functional-tests-when-run-through-ra/51935/7 "2005-12-04T15:04:56Z")

</div>

On 12/4/05, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Fixture Delete (0.001460) DELETE FROM users
> 
> ```
> if self.use_transactional_fixtures?
> ActiveRecord::Base.connection.execute("COMMIT")
> end
> 
> ```
> 
> Before and after (not at the same time…individual trials) the main  
> loop in the teardown method. Still getting errors for the tests.

Okay, turns out it should be a rollback, not a commit. I changed the  
teardown method to

# Add more helper methods to be used by all tests here…

def teardown  
self.class.fixture\_table\_names.reverse.each do |table\_name|  
klass\_name = Inflector.classify(table\_name.to\_s)  
if Object.const\_defined?(klass\_name)  
klass = Object.const\_get(klass\_name)  
klass.connection.delete(“DELETE FROM #{table\_name}”, ‘Fixture  
Delete’)  
else  
flunk(“Cannot find class for table ‘#{table\_name}’ to delete  
fixtures”)  
end  
end  
if self.use\_transactional\_fixtures?  
ActiveRecord::Base.connection.execute(“ROLLBACK”)  
end  
end

I guess rake isn’t doing the rollback in the functional tests? Not  
sure why really. The rollback is made when I do “ruby  
test/functional/test.rb” but not when I run it through rake. This  
means that the rollback is executed twice when I just run the test  
through ruby…but so far it doesn’t seem to be complaining.

Not sure if this is a bug in the rake tasks, or if I’m just  
misunderstanding what’s supposed to happen. Hopefully someone who  
knows more can shed some more light.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 4, 2005, 3:04pm UTC](https://www.ruby-forum.com/t/fixtures-not-working-in-functional-tests-when-run-through-ra/51935/8 "2005-12-04T15:04:55Z")

</div>

Fun and interesting new development. If I set  
use\_transactional\_fixtures to false, all my tests run fine using rake.  
They run pretty slowly, but at least they’re running now. I got the  
idea to do this after checking the test log file. With transactional  
fixtures, I see:

SQL (0.001503) BEGIN  
User Load (0.008182) SELECT \* FROM users WHERE (users.id = 1001)  
LIMIT 1  
Fixture Delete (0.001562) DELETE FROM messages  
Fixture Delete (0.005275) DELETE FROM tickets  
Fixture Delete (0.001460) DELETE FROM users  
Fixture Delete (0.001313) DELETE FROM categories  
SQL (0.001495) BEGIN  
User Load (0.012900) SELECT \* FROM users WHERE (users.id = 1001)  
LIMIT 1  
Fixture Delete (0.001634) DELETE FROM messages  
Fixture Delete (0.001371) DELETE FROM tickets  
Fixture Delete (0.001534) DELETE FROM users  
Fixture Delete (0.001459) DELETE FROM categories

Something about that doesn’t look right. Maybe it’s that the custom  
teardown isn’t committing the transaction? I’ve tried throwing a

```
if self.use_transactional_fixtures?
  ActiveRecord::Base.connection.execute("COMMIT")
end

```

Before and after (not at the same time…individual trials) the main  
loop in the teardown method. Still getting errors for the tests.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 4, 2005, 3:09pm UTC](https://www.ruby-forum.com/t/fixtures-not-working-in-functional-tests-when-run-through-ra/51935/9 "2005-12-04T15:09:01Z")

</div>

On 12/4/05, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Just so I can continue the trend of responding to my own thread…I  
> was trying to reproduce the error and figure out exactly what the  
> error messages were. I commented out the teardown method, ran rake,  
> and got no errors. Now I’m just thoroughly confused. I’ve got  
> absolutely none of the code or solutions that are described at  
> [http://dev.rubyonrails.org/ticket/2404](http://dev.rubyonrails.org/ticket/2404) yet I’m getting no errors.  
> _sigh_ Guess I’ll just spend lots of time with svn diff to see what’s  
> changed.

Go figure, they just decided to randomly stop working all of a sudden.  
I have absolutely no clue how to diagnose any of this. Giving up on  
it for the night…hopefully someone has an idea.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 4, 2005, 3:04pm UTC](https://www.ruby-forum.com/t/fixtures-not-working-in-functional-tests-when-run-through-ra/51935/10 "2005-12-04T15:04:57Z")

</div>

Just so I can continue the trend of responding to my own thread…I  
was trying to reproduce the error and figure out exactly what the  
error messages were. I commented out the teardown method, ran rake,  
and got no errors. Now I’m just thoroughly confused. I’ve got  
absolutely none of the code or solutions that are described at  
[http://dev.rubyonrails.org/ticket/2404](http://dev.rubyonrails.org/ticket/2404) yet I’m getting no errors.  
_sigh_ Guess I’ll just spend lots of time with svn diff to see what’s  
changed.

Pat
