Ps_gem Sh_gem wrote:

> Hi all, I am new to Ruby on Rails and struggling to finish my class
> project.
> I have problem with unit testing, if any body could help.

Professor Phlip will now dispense some tips.

> I have a function that updates 'feeds' table. It has a columns: id,
> feeddata, lastupdate. feedupdate.

Don't dupe the name of feeds into the feeds fields - feeddata should be data. 
And reconsider 'data' - what is the _intent_ of its variable? Where will it 
come 
out into the world and do something?

Next, "script/generate model" will generate created_on and updated_on 
automatically for you. Maybe they are already there; and maybe this assignment 
intends to see if you will use them.

> The function in method feed.rb is as follows:
 >
>  def Feed.updatefeed(feed_id)

update_feed. The goal is to match natural language whenever possible, so break 
words up with underbars.

>      @u = Feed.find(feed_id)

I don't think you will use @u with the same value in any other method (also, 
you 
are in a class method, so the @u lives in essentially the metaclass of the Feed 
class, or something like that). Call it 'feed'.

>      #response=fetch(@u.sourcelink)
>      #[email protected]=response.body

Put spaces around binary operators like =.

>      @u.lastupdate = Time.now
>      @u.save!
>   end
> -------------------------------------------------------------
> my test feed_test.rb is as follows:
> 
> class FeedTest < ActiveSupport::TestCase
>  # Replace this with your real tests.

Please delete all code that is not useful - including that comment, and 
including the test_truth stub.

>   fixtures :feeds
> 
>   def test_truth
>     assert true
>   end
> 
>   def test_updatefeed
>      feed1 = Feed.find(:first,:conditions=>["id=?",12])
>      Feed.updatefeed(feed1.id)
>      assert_equal(Time.now, feed1.lastupdate)

After you .reload the feed1, you will discover you cannot compare floats for 
equality. A time is a float, and the time will increment by a microsecond or so 
after the "money line" fires.

Try this:

       feed = feeds(feed_1)
       feed.update_attribute :updated_at, 1.hour.ago # spike
       Feed.update_feed(feed.id)
       feed.reload
       assert 1.second.ago < feed.updated_on &&
                 feed.updated_on < 1.second.from_now

Note I dropped the (). That is to rub the nose of every Java user how easy Ruby 
makes unit tests, especially assertions.

My assertion is a variation on a common theme - assert_close, or 
assert_in_delta. The point is to ensure a floating point number falls inside a 
tiny range; larger than common software math rounding errors, and larger than 
the time one test takes to execute. It still uses wall-clock time, not CPU 
time, 
so if your CPUs were loaded down with work the assertion might fail, but tests 
typically have the computer to themselves so we won't worry about that.

>   end
> 
> end

> feed_1:
>   id: 1

If your Rails version is > 2.0.0, drop the id: 1, and use automatically 
generated ids.

>   sourcelink: http://sports.yahoo.com/ncaaf/teams/aad/ical.ics

If the feed deals with yahoo, don't name it :feed_1. Name it :yahoo

-- 
   Phlip


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to