Last active
August 29, 2015 14:10
-
-
Save paulghaddad/300c5a2992243132eb93 to your computer and use it in GitHub Desktop.
Level Up 3: Writes meaningful tests, and knows what tautological tests are
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
Suppose we have a Rails model: Article | |
In the following tautological test, we aren't testing the the #camel_case_title method is actually doing anything: | |
describe Article, '#camel_case_title' do | |
it 'returns the title in camel case format' do | |
article = double(:article) | |
article.stub(:camel_case_title).and_return("The_Best_Article") | |
expect(article.camel_case_title).to eq("TheBestArticle") | |
end | |
end | |
But if you look at the implementation of this method in the model, it looks like this: | |
class Article < ActiveRecord::Base | |
def camel_case_title | |
title.camelize # This uses ActiveSupport's camelize method | |
end | |
end | |
If you notice, the real method should return "TheBestArticle", but our stubbed version returns "The_Best_Article". Thus, the test isn't actually testing the implementation of this model method. In fact, it is simply testing that stubbing works. | |
In the correct implementation of the test, we would create an article with a title attribute. Then we would exercise the #camel_case_title method on this instance, and confirm the camelized title is returned: | |
describe Article, '#camel_case_title' do | |
it 'returns the title in camel case format' do | |
article = Article.create(title: "The best article") | |
expect(article.camel_case_title).to eq("TheBestArticle") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment