Forum: Ruby on Rails Testing controllers for DB changes?

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.
F74253f4a099258870157426b4cdb2dc?d=identicon&s=25 davetron5000 (Guest)
on 2008-11-16 23:18
(Received via mailing list)
New to Ruby and to Rails (but not to development or testing).  Perhaps
I'm going about this wrong, but I'm writing a functional test of my
controller.  The controller makes changes to the item being operated
on and saves those changes to the database.  My test code doesn't
detect these changes unless I reload from the database, e.g.:

def test_something
  foo = foos(:my_test_foo)
  post :update, :id => foo.id, :bar => "Some Data"

  assert_equal("Some Data",foo.bar) # FAILS
  foo = Foo.find(foo.id)
  assert_equal("Some Data",foo.bar) # PASSES
end

I don't necessarily have a problem with this, but it made me wonder if
I'm approaching testing the wrong way.  Am I?

Dave
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-16 23:21
(Received via mailing list)
On Nov 16, 9:43 pm, davetron5000 <davetron5...@gmail.com> wrote:
> New to Ruby and to Rails (but not to development or testing).  Perhaps
> I'm going about this wrong, but I'm writing a functional test of my
> controller.  The controller makes changes to the item being operated
> on and saves those changes to the database.  My test code doesn't
> detect these changes unless I reload from the database, e.g.:
>

That's the way it is. the controller will have been handling a
different instance of Foo (that happens to correspond to the same row
in the database). The instance in your test is not aware of what has
been done to the other instance.

Fred
561319f16a08e221a31d13f4419a7de4?d=identicon&s=25 Pradeep Gatram (pradeepgatram)
on 2008-11-18 17:05
(Received via mailing list)
Fred is correct. A small suggestion to your code. Instead of

foo = Foo.find(foo.id)

simply use
foo.reload
This topic is locked and can not be replied to.