Skip to content

Instantly share code, notes, and snippets.

@dnch
Created December 12, 2013 03:42
Show Gist options
  • Save dnch/7922872 to your computer and use it in GitHub Desktop.
Save dnch/7922872 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe API::Decorator do
describe ".safe_attributes" do
# class WidgetDecorator < API::Decorator
# safe_attributes :safe
# end
#
# widget = Widget.new(safe: "foo", unsafe: "bar")
#
# decorated_widget = WidgetDecorator.new(widget)
# decorated_widget.to_json # => { "safe": "foo" }
it "uses the attribute filter when generating JSON" do
# expected result from json
attrs = { safe: "AAA" }
# fake our record and stub the call to as_json
widget = double("Widget")
widget.stub(:as_json) { ActiveSupport::JSON.encode(attrs) }
# and set up some message expectations
expect(widget).to receive(:as_json).with(:safe, :also_safe)
# configure our decorator
decorator = Class.new(API::Decorator)
decorator.safe_attributes :safe
# and uses it to generate some JSON
outcome = ActiveSupport::JSON.decode(decorator.new(widget).to_json)
expect(outcome).to have_key(:safe)
expect(outcome).to_not have_key(:unsafe)
expect(outcome[:safe]).to eq "AAA"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment