【问题标题】:Rspec model methods return nil when created by let当由 let 创建时,Rspec 模型方法返回 nil
【发布时间】:2014-07-22 14:14:29
【问题描述】:

好的,所以这看起来像是一个新手问,但是当我使用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,使用带有 postgis 2 的 postgres 9.3,并且严重依赖于 activerecord-postgis-adaptor、RGeo、activerecord-postgres-hstore 以及其他 gem。

有什么想法吗?

【问题讨论】:

  • @UriAgassi Rspec 日志显示 ROLLBACK。还有其他问题...可能是验证..
  • @ArupRakshit ROLLBACK 可能与DatabaseCleaner 政策有关,我不认为这是问题的一部分。
  • 一旦你得到答案,请ping我..我也想看看这个神秘错误的原因:-)
  • @PeterAlvin 的回答有效。 ROLLBACK 可能是 DatabaseCleaner 策略。

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


【解决方案1】:

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

parcel.municipal_id.nil?.should_not be_true

因为它是猴子修补所有对象,但是,您在 RSpec 的 subject 上调用它并将结果(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 }

【讨论】:

  • 谢谢!我不知道新的 RSpec 语法。
猜你喜欢
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多