简体   繁体   English

尝试创建单元测试(Ruby on Rails)时,Test和Rails控制台上的结果不同

[英]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. 我在针对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: 它在我的Tract表中找到与输入的经纬度长对最接近的点(将存在联系):

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. 因为我知道@input_lat和@input_lng的值是表中一组点的对,所以这两个断言都应该返回true。 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. 经过检查,最近的距离确实从Tract模型返回了一些结果,但最近的值.nast.lat的值为nil,其他所有值也是如此。

The funny thing is, when I try to replicate this test using the rails console, the results are good: 有趣的是,当我尝试使用rails控制台复制此测试时,结果很好:

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! 谢谢!

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. Rails控制台将使用development数据库,而测试将使用您的test数据库,这可能有所不同。

To add objects to your test database, you can use fixtures or factories . 要将对象添加到测试数据库中,可以使用Fixturefactory

A couple great gems for figuring out what's happening in your tests are byebug (Ruby 2 only) and pry-remote . Byebug (仅适用于Ruby 2)和pry -remote可以用来判断测试中发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM