I have a one-to many relationship in Rails:
class User < ActiveRecord::Base
has_many :activities, :order => "added_at DESC"
class Activity < ActiveRecord::Base
belongs_to :user
I have a method in Activity:
def self.test_message(user, message)
user.activities << Activity.create do |activity|
activity.message = message
activity.added_at = Time.now
end
end
and the following unit test:
require 'test_helper'
class ActivityTest < ActiveSupport::TestCase
def test_test_message
#From fixture
alice = User.find_by_name("alice")
assert_equal 0, alice.activities.count
Activity.test_message(alice, "Hello")
assert_equal 1, alice.activities.count
Activity.test_message(alice, "Goodbye")
assert_equal 2, alice.activities.count
assert_equal "Hello", alice.activities.find(:first).message
#The following line fails with: Goodbye expected but was Hello
assert_equal "Goodbye", alice.activities.find(:last).message,
acts = alice.activities
assert_equal 2, acts.count
assert_equal "Goodbye", acts[1].message
end
end
which fails on the indicated line, but I can't work out why.
Also, using activities.find(:last) works when using the development environment, but only fails under the test environment. I have dropped and rebuilt the database.