Last active
October 21, 2015 03:58
-
-
Save d1rtyvans/d4b21bf6270179dac687 to your computer and use it in GitHub Desktop.
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
# Test all of your attr_readers for any class (RSPEC) | |
def test_getters object, expected, dont_test=[] | |
object.instance_variables.each do |attribute| | |
attribute = attribute.to_s and attribute[0] = '' | |
unless dont_test.include? attribute | |
describe "##{attribute}" do | |
it "returns correct #{attribute}" do | |
expect(object.send(attribute)).to eq(expected[attribute.to_sym]) | |
end | |
end | |
end | |
end | |
end | |
# Example | |
args = { | |
name: 'item', | |
description: 'sweet item', | |
price: 2000, | |
qty: 60 | |
} | |
item = Item.new(args) | |
# Lets say Item has a setter for #on_sale that we don't want to test | |
# all we have to do is add #on_sale to the dont_test third argument | |
describe Item, 'getter methods' do | |
test_getters(item, args, ['on_sale']) # on_sale won't be tested | |
end | |
# The output of the tests should look something like this | |
Item getter methods | |
#name | |
returns correct name | |
#description | |
returns correct description | |
#price | |
returns correct price | |
#qty | |
returns correct qty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment