Created
December 12, 2013 03:42
-
-
Save dnch/7922872 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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