Last active
August 29, 2015 14:00
-
-
Save carpodaster/11402772 to your computer and use it in GitHub Desktop.
ActiveModel::Lint for rspec
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
# 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