# Trouble writing to db in test

**URL:** <https://www.ruby-forum.com/t/trouble-writing-to-db-in-test/119077>\
**Category:** Rails\
**Created:** [November 2, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/trouble-writing-to-db-in-test/119077 "2007-11-02T21:37:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [November 2, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/trouble-writing-to-db-in-test/119077/1 "2007-11-02T21:37:16Z")

</div>

I am having trouble testing the following controller method which takes  
an xml\_http\_request to create a new User object:

class ClientsController \< ApplicationController

def create\_user  
@client = Client.find(params[:id])  
user = User.new(params[:user])  
user.client\_id = @client.id  
if user.save  
@new\_user = user  
return if request.xhr?  
render :partial =\> ‘users’  
end  
end

The create\_user method is called from the ‘show’ page. On the first line  
of the test, I confirm that I am on the ‘show’ page. On the second line  
I feed in the xml\_http\_request to create a new User object:

class UserStoriesTest \< ActionController::Integration Test

```
fixtures :clients, :users

def test_create_user
    assert_template 'show'
    xml_http_request 'clients/create_user', :id => 1, :user => {

```

:name =\> ‘David’, :email =\> ‘removed\_email\_address@domain.invalid’, :password =\> ‘ruby’}  
end

The test passes without any errors, but no new User is created in the  
test database. What might be preventing this? Is it possible to write  
new data to the test db with fixtures already there?

Thanks,

Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [November 3, 2007, 2:31am UTC](https://www.ruby-forum.com/t/trouble-writing-to-db-in-test/119077/2 "2007-11-03T02:31:22Z")

</div>

UPDATE:

I’ve discovered that this issue is not unique to this controller method.  
I’m not able to write to my test database at all with anything other  
than fixtures. I’m positive I’ve executed other methods (as evidenced by  
files they alter) that create new rows in the development and production  
db when tested manually, but not in test db with any integration test.  
Any ideas?

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [November 3, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/trouble-writing-to-db-in-test/119077/3 "2007-11-03T23:11:14Z")

</div>

On 2 Nov 2007, at 20:37, Peter M. wrote:

> The test passes without any errors, but no new User is created in the  
> test database. What might be preventing this? Is it possible to write  
> new data to the test db with fixtures already there?

Are you checking your database after the test has run ? tests are  
wrapped in a transaction which is rolled back at the end of the test-  
changes to the db made by the test don’t persist (and depending on  
your db settings probably aren’t visible to other connections to the  
databases).

Fred

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [November 4, 2007, 8:05am UTC](https://www.ruby-forum.com/t/trouble-writing-to-db-in-test/119077/4 "2007-11-04T08:05:41Z")

</div>

Frederick C. wrote:

> Are you checking your database after the test has run ? tests are  
> wrapped in a transaction which is rolled back at the end of the test-  
> changes to the db made by the test don’t persist (and depending on  
> your db settings probably aren’t visible to other connections to the  
> databases).
> 
> Fred

I am checking the test database after the test is run. If changes to the  
test db are rolled back after the test, that would certainly explain it.  
I see can validate the existence of the ‘event’ objects created by using  
assert\_not\_nil(Event.find()) in my integration test. Thanks for  
clarifying this Fred 🙂
