Saturday, March 10, 2012
TDD Bootcamp Videos
You can watch the TDD Bootcamp videos on YouTube. It is over 5.5 hours of recording.
Tuesday, February 07, 2012
Testing and Command Query Separation Principle
The term 'command query separation' was coined by Bertrand Meyer in his book "Object Oriented Software Construction".
The fundamental idea is that we should divide an object's methods into two categories:
Queries: Return a result and do not change the observable state of the system (are free of side effects).
Commands: Change the state of a system but do not return a value.
Because the term 'command' is widely used in other contexts it is referred as 'modifiers' and 'mutators'.
It's useful if you can clearly separate methods that change state from those that don't. This is because you can use queries in many situations with much more confidence, changing their order. You have to be careful with modifiers.
The return type is the give-away for the difference. It's a good convention because most of the time it works well. Consider iterating through a collection in Java: the next method both gives the next item in the collection and advances the iterator. It's preferable to separate advance and current methods.
There are exceptions. Popping a stack is a good example of a modifier that modifies state. Meyer correctly says that you can avoid having this method, but it is a useful idiom. Follow this principle when you can.
From jMock home page: Tests are kept flexible when we follow this rule of thumb: Stub queries and expect commands, where a query is a method with no side effects that does nothing but query the state of an object and a command is a method with side effects that may, or may not, return a result. Of course, this rule does not hold all the time, but it's a useful starting point.
The fundamental idea is that we should divide an object's methods into two categories:
Queries: Return a result and do not change the observable state of the system (are free of side effects).
Commands: Change the state of a system but do not return a value.
Because the term 'command' is widely used in other contexts it is referred as 'modifiers' and 'mutators'.
It's useful if you can clearly separate methods that change state from those that don't. This is because you can use queries in many situations with much more confidence, changing their order. You have to be careful with modifiers.
The return type is the give-away for the difference. It's a good convention because most of the time it works well. Consider iterating through a collection in Java: the next method both gives the next item in the collection and advances the iterator. It's preferable to separate advance and current methods.
There are exceptions. Popping a stack is a good example of a modifier that modifies state. Meyer correctly says that you can avoid having this method, but it is a useful idiom. Follow this principle when you can.
From jMock home page: Tests are kept flexible when we follow this rule of thumb: Stub queries and expect commands, where a query is a method with no side effects that does nothing but query the state of an object and a command is a method with side effects that may, or may not, return a result. Of course, this rule does not hold all the time, but it's a useful starting point.
Saturday, February 04, 2012
Stubs are not Mocks - Concise Version of Martin Fowler's Article
Stubs - a common helper to testing environments. There is a difference in how test results are verified: a distinction between state verification and behavior verification. Focusing on one element of the software at a time -hence the common term unit testing. The problem is that to make a single unit work, you often need other units.
- Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
- Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).
- Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'.
- Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
Only mocks insist upon behavior verification. The other doubles can, and usually do, use state verification.
The classical TDD style is to use real objects if possible and a double if it's awkward to use the real thing. A mockist TDD practitioner, however, will always use a mock for any object with interesting behavior.
An acknowledged issue with state-based verification is that it can lead to creating query methods only to support verification. It's never comfortable to add methods to the API of an object purely for testing, using behavior verification avoids that problem.
Thursday, January 26, 2012
RCov Tips
1. How to turn off rcov for specific sections in a file:
Start and end with :
#:nocov:
in your file.
2. How to exclude files from rcov:
In spec directory, edit rcov.opts
append our path of files to –exclude option. Eg: –exclude “helpers/*,app/sweepers/*”.
Reference
http://psixty.wordpress.com/2010/06/22/how-to-exclude-files-in-rcova-code-coverage-tool-in-ruby/
Start and end with :
#:nocov:
in your file.
2. How to exclude files from rcov:
In spec directory, edit rcov.opts
append our path of files to –exclude option. Eg: –exclude “helpers/*,app/sweepers/*”.
Reference
http://psixty.wordpress.com/2010/06/22/how-to-exclude-files-in-rcova-code-coverage-tool-in-ruby/
Monday, January 16, 2012
How to uninstall all gems in global rvm ?
rvm use @global
rvm gemset empty global
If you want to remove all gems for a particular gemset, replace global with your-gemset-name
gem "rake" is not installed
Problem uninstalling rake? Run the following command:
rvm use @global && gem uninstall rake
rvm use @global && gem uninstall rake
Thursday, January 12, 2012
How to disable acts_as_audited 2.0 rails plugin in Rails 3.1 and RSpec
Create disable.rb under spec/support folder and call disable_auditing method on your modes. For example:
Account.disable_auditing
Cart.disable_auditing
Account.disable_auditing
Cart.disable_auditing
Monday, January 09, 2012
Using Reek gem in Rails 3.1 projects
1. gem install reek
2. reek lib/*.rb > myreport.txt
You can run reports for app/models/*.rb, app/controllers/*.rb and app/helpers/*.rb
2. reek lib/*.rb > myreport.txt
You can run reports for app/models/*.rb, app/controllers/*.rb and app/helpers/*.rb
rcov for Rails 3.1
1. rake spec:rcov
2. open coverage/index.html
3. add coverage/* to your .gitignore file.
4. $ rake -T rcov
rake spec:clobber_rcov # Remove rcov products for rcov
rake spec:rcov # Run all specs in spec directory with RCov (excluding plugin specs)
2. open coverage/index.html
3. add coverage/* to your .gitignore file.
4. $ rake -T rcov
rake spec:clobber_rcov # Remove rcov products for rcov
rake spec:rcov # Run all specs in spec directory with RCov (excluding plugin specs)
Rails Best Practices
1. gem install rails_best_practices
2. gem install ripper
3. From the root of the project run:
rails_best_practices -f html .
to generate the report.
2. gem install ripper
3. From the root of the project run:
rails_best_practices -f html .
to generate the report.
Sunday, December 25, 2011
You need to specify gem names as Strings. Use 'gem "test"' instead.
This can happen due to syntax error in your Gemfile. Fix the sytax error by manually checking this file. If that does not work delete the lines that you added to result in this problem and copy / paste from a working copy of a Gemfile.
Wednesday, December 07, 2011
syntax error, unexpected keyword_end
This happens because you have : <%= end %> instead of <% end %>
Thursday, December 01, 2011
`test': unknown command 't' (ArgumentError)
To write tests using Test::Unit new syntax :
test "truth" do
assert true
end
1. gem install test-unit
2. Code:
require 'rubygems'
gem 'test-unit'
require 'test/unit'
class ExampleTest < Test::Unit::TestCase
def test_truth
assert true
end
test "truth" do
assert true
end
end
3. Run: ruby example_test This will work on ruby-1.9.3-p0.
Reference: Test::Unit and MiniTest with different Ruby versions
test "truth" do
assert true
end
1. gem install test-unit
2. Code:
require 'rubygems'
gem 'test-unit'
require 'test/unit'
class ExampleTest < Test::Unit::TestCase
def test_truth
assert true
end
test "truth" do
assert true
end
end
3. Run: ruby example_test This will work on ruby-1.9.3-p0.
Reference: Test::Unit and MiniTest with different Ruby versions
Wednesday, November 30, 2011
Installing Ruby 1.9.3 using RVM on Mac OS Lion
ERROR: The provided CC(gcc) is LLVM based, it is not yet fully supported by ruby and gems, please read `rvm requirements`.
1) Download Xcode 4.2.x via App Store.
2) Go to Applications folder and click Install Xcode
3) Download and install GCC from https://github.com/kennethreitz/osx-gcc-installer
4) In a new terminal run : export CC=gcc
and then run : rvm install 1.9.3
5) You may also want to upgrade ruby gems by running:
gem update --system
1) Download Xcode 4.2.x via App Store.
2) Go to Applications folder and click Install Xcode
3) Download and install GCC from https://github.com/kennethreitz/osx-gcc-installer
4) In a new terminal run : export CC=gcc
and then run : rvm install 1.9.3
5) You may also want to upgrade ruby gems by running:
gem update --system
Wednesday, November 02, 2011
ActiveRecord::Relation, undefined method error
If you are using the active record relationships to access columns, for instance:
Article has many comments
article.comments.where("spam = ?", false).order("created_at asc")
This returns ActiveRelation object that does not fire the query. Only when you loop through the results you can access the columns of comments objects. If you want to force the call to the database just append "all" to it like this:
article.comments.where("spam = ?", false).order("created_at asc").all
Article has many comments
article.comments.where("spam = ?", false).order("created_at asc")
This returns ActiveRelation object that does not fire the query. Only when you loop through the results you can access the columns of comments objects. If you want to force the call to the database just append "all" to it like this:
article.comments.where("spam = ?", false).order("created_at asc").all
Monday, October 24, 2011
(local out of date) error in Git when working with branches
1. git pull origin your-branch
2. git push origin your-branch
Step 1 brings your local branch to up-to date. Step 2 pushes your changes to the remote branch.
2. git push origin your-branch
Step 1 brings your local branch to up-to date. Step 2 pushes your changes to the remote branch.
Sunday, October 23, 2011
invalid date format in specification: gem ruby
1) Upgraded ruby gems to 1.8.11 (gem update --system)
2) gem pristine --all
No luck.
Deleted all the gemspec that were causing problems: whenever-0.6.8.gemspec etc. Then bundle install made it work.
2) gem pristine --all
No luck.
Deleted all the gemspec that were causing problems: whenever-0.6.8.gemspec etc. Then bundle install made it work.
`build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)
I was getting :
Installing eventmachine (0.12.10) with native extensions .rvm/rubies/ruby-1.8.7-p330/lib/ruby/site_ruby/1.8/rubygems/installer.rb:533:in `build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)
during the installation of scheduler_daemon on Mac OS Lion. Upgraded XCode via appstore (download from appstore and install) to resolve this issue.
Installing eventmachine (0.12.10) with native extensions .rvm/rubies/ruby-1.8.7-p330/lib/ruby/site_ruby/1.8/rubygems/installer.rb:533:in `build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)
during the installation of scheduler_daemon on Mac OS Lion. Upgraded XCode via appstore (download from appstore and install) to resolve this issue.
Friday, October 07, 2011
Integrating Tweet Button in Rails App
In Rails 3.0 app:
1) In Gemfile include:
gem 'tweet-button'
2) bundle install
3) In application_helper.rb include:
module ApplicationHelper
include TweetButton
... other methods...
end
4) In your partial:
<%= tweet_button(:via => "CreditCardLogic", :url => polymorphic_url(article), :text => "Check this out!") %>
To see this in action, check out my credit cards site.
1) In Gemfile include:
gem 'tweet-button'
2) bundle install
3) In application_helper.rb include:
module ApplicationHelper
include TweetButton
... other methods...
end
4) In your partial:
<%= tweet_button(:via => "CreditCardLogic", :url => polymorphic_url(article), :text => "Check this out!") %>
To see this in action, check out my credit cards site.
Tuesday, September 27, 2011
Making a Branch the Master in GIT
git checkout cat_killer
git merge -s ours master
git checkout master
git merge cat_killer
git push
cat_killer is the branch you want to make the master.
Stolen from Stackoverflow post.
git merge -s ours master
git checkout master
git merge cat_killer
git push
cat_killer is the branch you want to make the master.
Stolen from Stackoverflow post.
Subscribe to:
Posts (Atom)