Forum: Ruby on Rails Unit tests dynamic finders

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Harm (Guest)
on 2006-03-08 20:59
Hi all,
I am having a problem with unit tests. Whenever I have a dependency
between two models (B depends on A) deleting A should also delete B.
This is simple with the dependent parameter in the model file of A. But
when I write a test the dependency seems to work, I can't find the
answer with id 1 anymore(Answer.find(1)). But I appearently _do_ find it
with Answer.find_by_question_id(1) or Answer.find(:first, :conditions =>
"question_id = 1"). What am I doing wrong??
Below you will find the relevant files used in testing.

questions.yml:
first_question:
  id: 1
  question: "Will fixture files work?"
  answer: "No"

answers.yml:
first_answer:
  id: 1
  question_id: 1

question_test.rb:
class QuestionTest < Test::Unit::TestCase
    fixtures :categories, :questions, :answers

    def test_destroy
        question = questions(:first)
        id = question.id
        question.destroy
        assert_raise(ActiveRecord::RecordNotFound) {Question.find(1)}
        assert_raise(ActiveRecord::RecordNotFound) {Answer.find(1)}
        assert_raise(ActiveRecord::RecordNotFound)
{Answer.find_by_question_id(1)} <<TROUBLE, ACTUALLY SUCCEEDS
        #assert_raise(ActiveRecord::RecordNotFound) {Answer.find(:all,
:conditions => "question_id = 1" )} <<AND AGAIN TROUBLE
    end
end

question.rb:
class Question < ActiveRecord::Base
    has_one :answer, :dependent => true
end

With many thanks,

Harm
This topic is locked and can not be replied to.