当let创建时,Rspec模型方法返回nil

时间:2014-06-02 12:47:21

标签: ruby-on-rails ruby postgresql rspec rgeo

好的,所以这似乎是一个全新的问题,但是当我用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属性被注释掉,因为在RGeo中在球形定义的多边形上调用.centroid方法是禁止的。在我的待办事项列表中获得正确的投影是不相关的对这个问题) 规范失败了:

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

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

我正在运行rails 3.2,使用postgres 9.3和postgis 2,并且在很大程度上依赖于activerecord-postgis-adapter,RGeo,activerecord-postgres-hstore以及其他宝石。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

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

parcel.municipal_id.nil?.should_not be_true

因为它的猴子修补了所有对象,你在RSpec的subject上调用它并将结果(Proc)作为一个块传递给{{ 1}},这是忽略它。该错误消息反映了正在评估的对象是主题nil?实例。

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

Parcel

或者如果您确实只想检查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 ,从RSpec 3开始将其分解为单独的gem,那么你可以用以下内容替换整个its调用:

it