Last active
December 15, 2015 11:29
-
-
Save brenes/5253378 to your computer and use it in GitHub Desktop.
Common shared examples 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
shared_examples_for "model with attributes" do |model_name| | |
context "validations" do | |
context "with valid values" do | |
let(:attributes) { attributes_for model_name } | |
let(:model_object) { build model_name, attributes } | |
subject { model_object } | |
it { should be_valid } | |
its(:errors) { should be_blank } | |
context "after save" do | |
before { model_object.save } | |
subject { model_object.class.first(order: "id DESC") } | |
FactoryGirl.attributes_for(model_name).keys.each do |attribute_name| | |
its(attribute_name) { should eq attributes[attribute_name] } | |
end | |
end | |
end | |
end | |
end |
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
shared_examples_for "model with has many relationship" do |model_name, related_model_factory, has_many_relationship, belongs_to_relationship| | |
context "when creating various #{has_many_relationship}" do | |
let(:model_object) { create model_name } | |
let(:related_object_1) { create related_model_factory, belongs_to_relationship => model_object } | |
let(:related_object_2) { create related_model_factory, belongs_to_relationship => model_object } | |
before do | |
related_object_1 && related_object_2 | |
end | |
subject { model_object } | |
its(has_many_relationship) { should match_array [related_object_1, related_object_2] } | |
context "related #{has_many_relationship}" do | |
subject { related_object_1 } | |
its(belongs_to_relationship) { should eq model_object } | |
end | |
context "related #{has_many_relationship}" do | |
subject { related_object_2 } | |
its(belongs_to_relationship) { should eq model_object } | |
end | |
end | |
end |
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
shared_examples_for "model with required attributes" do |model_name, required_attribute_names| | |
context "validations" do | |
# we go through the required attributes and for each one we create a context where it's set to nil and we check the object is not valid and there's an error on the attributes | |
required_attribute_names.each do |attribute_name| | |
context "with empty #{attribute_name}" do | |
let(:model_object) { build model_name, attribute_name => nil } | |
before { model_object.valid? } | |
subject { model_object } | |
it { should_not be_valid } | |
it { should have(1).error_on(attribute_name) } | |
it { subject.error_on(attribute_name).should include I18n.t('errors.messages.blank') } | |
end | |
end | |
end | |
end |
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
shared_examples_for "model with slug" do |model_name, sluggable_attribute| | |
let(:model_object) { create model_name, sluggable_attribute => "test name" } | |
context "after save" do | |
subject { model_object } | |
its(:slug) { should eq "test-name" } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment