Skip to content

Instantly share code, notes, and snippets.

@dnd
Last active August 29, 2015 14:24
Show Gist options
  • Save dnd/1376e015c0b4a4435558 to your computer and use it in GitHub Desktop.
Save dnd/1376e015c0b4a4435558 to your computer and use it in GitHub Desktop.
require 'test_helper'
require 'virtus'
require 'representable/json'
class NilFailTest < MiniTest::Spec
class Company
include Virtus.model
attribute :name, String
attribute :errors, Hash, default: nil
end
module CompanyRepresenter
include Representable::JSON
property :name
property :errors, render_nil: true
end
describe "After representing a nil value" do
it "representation should still work on subsequent attempts" do
first_company_without_errors = Company.new name: 'I will work because nil has not been represented'
CompanyRepresenter.represent(nil).to_hash rescue nil
# If `errors` is present the representation will work
company_with_errors = Company.new(name: 'I will work because I have non-nil errors', errors: {})
CompanyRepresenter.represent(company_with_errors).to_hash
# This line will fail. It looks like it has to do with `errors` being nil,
# and then trying to set a default value that is nil.
company_without_errors = Company.new name: 'I will fail because my errors default is used'
CompanyRepresenter.represent(company_without_errors).to_hash
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment