# IntegrationTest doesn't affect model

**URL:** <https://www.ruby-forum.com/t/integrationtest-doesnt-affect-model/156599>\
**Category:** Rails\
**Created:** [December 30, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/integrationtest-doesnt-affect-model/156599 "2008-12-30T18:30:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fritza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5f9b8f/32.png) [@fritza](https://www.ruby-forum.com/u/fritza)\
**Post date:** [December 30, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/integrationtest-doesnt-affect-model/156599/1 "2008-12-30T18:30:25Z")

</div>

At the end of this message, I have (trimmed) code for an  
IntegrationTest, controller, and model.

The IntegrationTest posts to a URL that should destroy a model object  
(“Secondary”). The Secondary belongs\_to a Principal. The Secondary is  
not deleted, nor is it removed from the Principal’s has\_many secondaries  
relationship. (See the FAILS comments in the IntegrationTest.)

The PeopleControllerTest functional test exercises the same action in  
the same way, and in that, the destroy succeeds. Running the application  
from a browser, for the same action, succeeds.

I’d like my integration test to test whether user actions have effect in  
the model. I understood that was the purpose. What is the cause of this  
problem, or how should I pursue it?

ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]  
Rails 1.2.6

```
-- F

```

# In IntegrationTest

def test\_existing\_login  
https!(true)

person = Principal.find\_by\_loginid(‘t-9fritz’)

# (omitted) log the person in and verify success

# There should be one secondary for this person. Delete it.

assert\_equal 1, person.secondaries.count  
n\_secondaries = Secondary.count  
sec\_id = person.secondaries.first.id  
post\_via\_redirect ‘/people/delete\_secondary/’ + sec\_id.to\_s

# (omitted) verify the redirect is as expected

assert\_equal n\_secondaries - 1, Secondary.count

# ^ FAILS. Secondary.count is unchanged.

assert\_raises(ActiveRecord::RecordNotFound) { Person.find(sec\_id) }

# ^ FAILS: The record is still in the DB.

assert\_equal 0, person.secondaries.count

# ^ FAILS: The secondaries list is unchanged.

end

# In PeopleController

def delete\_secondary  
@person = Person.find(params[:id])  
principal = @person.principal

```
flash[:notice] = "Deleted contact #{@person.full_name}"
@person.destroy
redirect_to :action => :show, :id => principal

```

end

# In person.rb

# people is the table; Principal and Secondary are single-table

inheritors.  
class Principal \< Person  
has\_many :secondaries, :class\_name =\> ‘Secondary’,  
:dependent =\> :destroy  
validates\_associated :secondaries, :allow\_nil =\> true

# …

end

class Secondary \< Person  
belongs\_to :principal

# …

end

---

<div class="post-metadata">

**Author:** ![fritza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5f9b8f/32.png) [@fritza](https://www.ruby-forum.com/u/fritza)\
**Post date:** [January 6, 2009, 10:29pm UTC](https://www.ruby-forum.com/t/integrationtest-doesnt-affect-model/156599/2 "2009-01-06T22:29:47Z")

</div>

Summary: I was trying to write an IntegrationTest. I did a  
post\_via\_redirect that ought to result in a record being deleted from my  
(fixture-primed) database. The same post worked as expected in the  
functional test. In the integration test, the record was not deleted.

My error: I had been using a session instance (returned by open\_session,  
with a couple of singleton methods) for the test. Once committed to the  
session object, I issued

```
post_via_redirect ...

```

instead of

```
session_inst.post_via_redirect ...

```

The convenient way ActionController::IntegrationTest makes the post\*,  
get\*, etc methods available blinded me to the fact that they were  
faÃ§ades for the same methods of an implicit session instance. So if I  
wanted them to apply to a session, I had to send them to that instance.

Obvious, now that I understand it.

And of like an idiot I “simplified” that detail out of my sample code.  
Sorry for the bandwidth.

```
â€” F
```
