Created
August 13, 2015 14:52
-
-
Save rilian/0f744e2f102349170351 to your computer and use it in GitHub Desktop.
structure for rails model test
This file contains hidden or 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
| describe ClassName do | |
| describe 'Database' do | |
| end | |
| describe 'Relations' do | |
| end | |
| describe 'Validations' do | |
| end | |
| describe 'Scopes' do | |
| end | |
| describe 'Class methods' do | |
| end | |
| describe 'Callbacks' do | |
| end | |
| describe 'Instance Methods' do | |
| end | |
| end |
Clean version:
describe ClassName do
describe 'Class' do
end
describe 'Instance' do
describe 'Validations'
describe 'Callbacks'
end
describe 'Database'
end
Author
improved
describe 'Class' do
pending
end
describe 'Instance' do
pending
end
describe 'Validations' do
pending
end
describe 'Callbacks' do
pending
end
# TODO: move to a spec/database folder
describe 'Database' do
pending
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I fully agree with saaji
This applies only to model tests, as testing of objects that do not depend on database or other 3rd-party code is a completely different story with a lot more advanced tools available to organise code and tests.
As a small compromise we can imagine that any model is actually 2 objects: Class and Instance, thus they may have separate sections.
Main reason is to minimise maintenance cost of tests but decreasing test fragility.
"Fragile Tests increase the cost of test maintenance by forcing us to visit many more tests each time we modify the functionality of the system or the fixture. It is particularly deadly on projects that do highly incremental delivery" - http://xunitpatterns.com/Fragile%20Test.html
Tests that over-specify implementation are fragile because they will fail if we change implementation mechanism. Examples of that are refactoring of scope to class method or callback to wrapper method.
It is not necessary. As @rilian told, sections are needed to locate tests for big models. Big models is a problem that should be fixed. Tests that are difficult to locate because there are too many of them is only a symptom of this problem, they should not cover it.
They force to write separate tests for every method. But methods may be a result of refactoring, thus they will be covered in a separate group of tests, possibly for another object. If we then write additional tests for this particular method, it again creates a problem of fragile tests - we'd have to change multiple tests if we change 1 method.
@rilian has mentioned that if we remove the original code under test, we will be left with separated methods without any tests. While it is true, that is not a big problem. When we remove code, tests for that code will fail. We will then remove tests.