Skip to content

Instantly share code, notes, and snippets.

@jsanders
Created August 28, 2012 17:04
Show Gist options
  • Save jsanders/3500774 to your computer and use it in GitHub Desktop.
Save jsanders/3500774 to your computer and use it in GitHub Desktop.
Rspec shared examples for ActiveModel lint
shared_examples "ActiveModel" do
class BeBoolean
def matches?(it)
@it = it
it.kind_of?(TrueClass) || it.kind_of?(FalseClass)
end
def failure_message
"expected #{@it} to be either true or false"
end
end
def be_boolean
BeBoolean.new
end
describe "Class" do
subject { described_class }
it { should respond_to(:model_name) }
describe "#model_name" do
it("is a String") { subject.model_name.should be_kind_of(String) }
it("responds to human") { subject.model_name.should respond_to(:human) }
context "#human" do
it("is an String") { subject.model_name.human.should be_kind_of(String) }
end
it("responds to singular") { subject.model_name.should respond_to(:singular) }
context "#singular" do
it("is an String") { subject.model_name.singular.should be_kind_of(String) }
end
it("responds to plural") { subject.model_name.should respond_to(:plural) }
context "#plural" do
it("is an String") { subject.model_name.plural.should be_kind_of(String) }
end
end
end
describe "Instance" do
subject { described_class.new }
it { should respond_to(:persisted?) }
describe "#persisted" do
it("is boolean") { subject.persisted?.should be_boolean }
end
it { should respond_to(:to_key) }
describe "#to_key" do
context "when #persisted? is false" do
before { def subject.persisted?() false end }
it("is nil") { subject.to_key.should be_nil }
end
context "when #persisted? is true" do
before { def subject.persisted?() true end }
it("is Enumerable") { subject.to_key.should be_kind_of(Enumerable) }
end
end
it { should respond_to(:to_param) }
describe "#to_param" do
context "when #persisted? is false" do
before { def subject.persisted?() false end }
it("is nil") { subject.to_param.should be_nil }
end
context "when #persisted? is true" do
before { def subject.persisted?() true end }
it("is a String") { subject.to_param.should be_kind_of(String) }
end
end
it { should respond_to(:valid?) }
describe "#valid?" do
it("is boolean") { subject.valid?.should be_boolean }
end
it { should respond_to(:to_partial_path) }
describe "#to_partial_path" do
it("is a String") { subject.to_partial_path.should be_kind_of(String) }
end
it { should respond_to(:errors) }
describe "#errors" do
it("is an Array") { subject.errors.should be_kind_of(Array) }
it("responds to full_messages") { subject.errors.should respond_to(:full_messages) }
context "#full_messages" do
it("is an Array") { subject.errors.full_messages.should be_kind_of(Array) }
end
end
end
end
class ::Something
def self.model_name
model_name = ''
def model_name.human() '' end
def model_name.singular() '' end
def model_name.plural() '' end
model_name
end
def persisted?
false
end
def to_key
persisted? ? [] : nil
end
def to_param
persisted? ? '' : nil
end
def valid?
false
end
def to_partial_path
''
end
def errors
errors = []
def errors.full_messages() [] end
errors
end
end
require 'something'
require 'shared_examples_for_active_model'
describe ::Something do
it_behaves_like "ActiveModel"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment