Yael Herrera Halley
Yael Herrera Halley

Reputation: 1203

Different results on Test and in rails console when trying to create a unit test (Ruby on Rails)

I've been having problems with a test on a helper for a model on Ruby 2.0.0-p481, Rails 4.1.1, PostgreSQL 9.4 DB.

My helper is very simple. It finds on my Tract table the point(s) that are nearest (ties will exist) to the inputed lat, long pair:

module TractsHelper

  # returns rows (precalculated data) from the census tract point that is nearest given lat/log location
  def self.find_nearest(params)
    raise ArgumentError, 'Must supply starting latitude'  unless !params[:lat].nil?
    raise ArgumentError, 'Must supply starting longitude' unless !params[:lon].nil?

    nearest = Tract.select(:lat, :lon).order("(lat - #{params[:lat]})*(lat - #{params[:lat]}) + (lon - #{params[:lon]})*(lon - #{params[:lon]})").first

    if nearest.nil?
      tracts = Tract.none
    else
      tracts = Tract.where({:lat => nearest.lat, :lon => nearest.lon})
      #tracts = Tract.order("ST_Distance(POINT(#{params[:lat]}, #{params[:lon]}), POINT(lat, lon))")
    end
    return tracts
  end

end

Now, when I tried to write a test, this is what I came up with:

class TractsHelperTest < ActionView::TestCase
    def setup
          @input_lat = 47.7225472
          @input_lng = -122.2818937
    end

    test "return nearest census tract" do

        nearest_tract = TractsHelper.find_nearest({:lat => @input_lat, :lon => @input_lng})

        assert_equal @input_lat, nearest_tract.first.lat.to_f
        assert_equal @input_lng, nearest_tract.first.lon.to_f
    end
end

Since I know the values of @input_lat and @input_lng are a pair for a set of points in my table, this should return true for both assertions. However, I get:

Finished in 0.200984s, 4.9755 runs/s, 4.9755 assertions/s.

  1) Failure:
TractsHelperTest#test_return_nearest_tract [/home/ubuntu/workspace/myapp/test/helpers/tracts_helper_test.rb:13]:
Expected: 47.7225472
Actual: 0.0

1 runs, 1 assertions, 1 failures, 0 errors, 0 skips

Upon inspection, nearest_tract does return some results from the Tract model, but the value for nearest_tract.first.lat is nil, and so is every other value.

The funny thing is, when I try to replicate this test using the rails console, the results are good:

2.0.0-p481 :118 > @input_lat = 47.7225472
2.0.0-p481 :119 > @input_lng = -122.2818937
2.0.0-p481 :120 > nearest_tract = TractsHelper.find_nearest({:lat => @input_lat, :lon => @input_lng});
2.0.0-p481 :121 > @input_lat== nearest_tract.first.lat.to_f
 => true 
2.0.0-p481 :122 > @input_lng== nearest_tract.first.lon.to_f
 => true 

Which is really weird. Could anyone share some insights as to why the result is different depending on where I run the command / how to handle it?

Thanks!

Upvotes: 0

Views: 121

Answers (1)

Josh
Josh

Reputation: 8596

It looks like what your testing is dependent on what is stored in your database.

The rails console will be using the development database while the tests will use your test database, which is likely different.

To add objects to your test database, you can use fixtures or factories.

A couple great gems for figuring out what's happening in your tests are byebug (Ruby 2 only) and pry-remote.

Upvotes: 1

Related Questions