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