Skip to content

Instantly share code, notes, and snippets.

@theodorton
Last active December 18, 2015 21:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theodorton/5848796 to your computer and use it in GitHub Desktop.
Save theodorton/5848796 to your computer and use it in GitHub Desktop.
Support file for RSpec aimed at DRYing up ActiveModel Serializer specs.
describe BlogPostSerializer do
include_context "serializer spec"
let(:model) do
BlogPost.new({
title: "Lol",
author_attributes: { email: "john@doe.com"}
})
end
within('blog_post') do
its(['title']).should eq('Lol')
within('author') do
its(['email']).should eq('john@doe.com')
end
end
end
# spec/shared/serializer_spec.rb
shared_context "serializer spec" do
let(:serialized_model) { described_class.new(model).as_json.with_indifferent_access }
subject { serialized_model }
def self.within(*scopes, &blk)
context "within" do
around(:each) do |example|
original_scopes = (@scopes ||= [])
@scopes += scopes
example.run
@scopes = original_scopes
end
subject do
@scopes.inject(serialized_model) do |model, scope|
model[scope]
end
end
describe "#{scopes.join(' ')}", &blk
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment