2

好的,所以这看起来像是一个新手问,但是当我创建一个我的模型实例时let,由于某种原因,它上面的所有 getter 方法都返回 nil。这是我的例子:

模型:

class Parcel < ActiveRecord::Base
  extend UUIDHelper

  serialize :address, ActiveRecord::Coders::Hstore

  self.rgeo_factory_generator = RGeo::Geographic.spherical_factory srid: 3785

  attr_accessible :address, :area_poly, :id, :lonlat, :municipal_id
end

规格:

require 'spec_helper'
require 'street_address'

describe Parcel do
  let(:geo_factory) { RGeo::Geographic.spherical_factory(srid: 3785)}
  let(:point1) {geo_factory.point(-72.9229165, 41.3096987)}
  let(:point2) {geo_factory.point(-72.9229169, 41.3096987)}
  let(:point3) {geo_factory.point(-72.9229169, 41.3096983)}
  let(:point4) {geo_factory.point(-72.9229165, 41.3096983)}
  let(:line_string) {geo_factory.line_string([point1,point2,point3,point4])}
  let(:polygon) {geo_factory.polygon(line_string)}
  let(:parcel) { Parcel.create(
      municipal_id: 'abc123',
      area_poly: polygon,
      #lonlat: polygon.centroid,
      address: StreetAddress::US.parse('55 Whitney Ave New Haven, CT 06510').as_json
  )}

  it "#municipal_id should not be nil" do
    parcel.municipal_id.nil? should_not be_true
  end
end

(#longlat 属性被注释掉,因为.centroid在 RGeo 中对球形定义的多边形调用该方法是不行的。在我的待办事项列表中获得正确的投影与这个问题无关)并且规范失败像这样:

expected: non-true value
  got: #<Parcel id: nil, municipal_id: nil, address: {}, created_at: nil, updated_at: nil, area_poly: nil, lonlat: nil>

FWIW,这是 Rspec 日志:

Connecting to database specified by database.yml
   (0.1ms)  BEGIN
   (7.2ms)  SELECT * FROM geometry_columns WHERE f_table_name='parcels'
   (0.1ms)  SAVEPOINT active_record_1
  SQL (4.2ms)  INSERT INTO "parcels" ("address", "area_poly", "created_at", "id", "lonlat",     "municipal_id", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7)  [["address", "\"number\"=>\"55\",\"street\"=>\"Whitney\",\"street_type\"=>\"Ave\",\"unit\"=>NULL,\"unit_prefix\"=>NULL,\"suffix\"=>NULL,\"prefix\"=>NULL,\"city\"=>\"New Haven\",\"state\"=>\"CT\",\"postal_code\"=>\"06510\",\"postal_code_ext\"=>NULL"], ["area_poly", #<RGeo::Geographic::SphericalPolygonImpl:0x80b773e0 "POLYGON ((-72.9229165 41.3096987, -72.9229169 41.3096987, -72.9229169 41.3096983, -72.9229165 41.3096983, -72.9229165 41.3096987))">], ["created_at", Mon, 02 Jun 2014 11:59:51 UTC +00:00], ["id", nil], ["lonlat", nil], ["municipal_id", "abc123"], ["updated_at", Mon, 02 Jun 2014 11:59:51 UTC +00:00]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  ROLLBACK

作为参考,我可以Parcel.create在 Rails 控制台中成功运行,并获取实例变量以返回非零值

我正在运行 rails 3.2,使用带有 postgis 2 的 postgres 9.3,并且严重依赖于 activerecord-postgis-adaptor、RGeo、activerecord-postgres-hstore 等。

有什么想法吗?

4

1 回答 1

3

你错过了一个时期。should_not旨在发送到您正在检查其值的对象,如:

parcel.municipal_id.nil?.should_not be_true

但是,因为它是在所有对象上修补的猴子,所以您在 RSpec 上调用它subject并将结果 (a Proc) 作为块传递给nil?,而忽略它。错误消息反映正在评估的对象是主题Parcel实例。

顺便说一句,请注意 RSpec 中当前和首选的语法是:

expect(parcel.municipal_id.nil?).to_not be_truthy  # be_true was renamed to be_truthy in RSpec 3

或者如果您真的只想检查以下值true

expect(parcel.municipal_id.nil?).to_not be(true)

或者如果你想要更紧凑:

expect(parcel.municipal_id).to_not be_nil

或者如果你更喜欢莎士比亚的感觉:

expect(parcel.municipal_id).to be

或者,如果您想利用its从 RSpec 3 开始将其分解为单独的 gem,那么您可以将整个it调用替换为:

its(:municipal_id) { should_not be_nil }
于 2014-06-02T13:39:33.880 回答