IntegrationTest doesn't affect model


#1

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


#2

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