Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@carpodaster
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carpodaster/11402772 to your computer and use it in GitHub Desktop.
Save carpodaster/11402772 to your computer and use it in GitHub Desktop.
ActiveModel::Lint for rspec
# Provides an rspec version https://github.com/rails/rails/blob/master/activemodel/lib/active_model/lint.rb
# Approaches like https://gist.github.com/msgehard/910773 do not seem to work with
# current versions of minitest.
#
# Usage:
# 1. Put file in spec/support
# 2. Add it_behaves_like 'ActiveModel' in your spec file
shared_examples_for 'ActiveModel' do
let(:model) { subject }
describe '#to_key' do
it 'returns nil if not persisted' do
expect(model).to respond_to :to_key
model.stub(:persisted?).and_return(false)
expect(model.to_key).to be_nil
end
end
describe '#to_param' do
it 'returns nil if not persisted' do
expect(model).to respond_to :to_param
model.stub(:persisted?).and_return(false)
expect(model.to_param).to be_nil
end
end
describe '#to_partial_path' do
it 'returns a string' do
expect(model.to_partial_path).to be_kind_of String
end
end
describe '#persisted?' do
it 'returns a boolean' do
expect(model.persisted?).to satisfy { |b| [true, false].include?(b) }
end
end
describe '.model_name' do
it 'responds to to_str' do
expect(model.class.model_name).to respond_to :to_str
end
[:human, :singular, :plural].each do |o|
it "#{o} responds to to_str" do
expect(model.class.model_name.send(o)).to respond_to :to_str
end
end
end
describe '#errors' do
it 'returns a hash with arrays' do
expect(model.errors[:hello]).to be_a Array
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment