Add Testing to Your Gem
There’s the old post about how to create a ruby gem.
This post is focus on how to add testing to gem.
First, add rspec into your gem dependency. I like to use rspec as my testing framework.
Here i also use webmock to mock my http request during spec testing.
1 2 3 4 5 | |
Create a rake task to run rspec. I create rspec.rake under {gem_root_folder}/tasks/.
Set the default task to :spec then i can just type rake to run testing.
In the meantime i set --color -f d to output result with color and document format.
1 2 3 4 5 6 7 8 | |
In order to exec our rspec.rake. Import all .rake file under tasks folder to Rakefile.
1 2 3 | |
Now we could start to write testing.
Create {gem_root_folder}/spec/ folder and spec_helper.rb under it.
1 2 3 4 5 | |
Create mvn_utils_spec.rb under {gem_root_folder}/spec/ and require spec_helper.
We could require all targets in spec_helper.rb and each spec.rb file just require spec_helper.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
In the above example, i test MvnUtils.find_latest_version and expect the value should be 1.0.100.
This function will inovke http request to mvn server but i use stub request here to prevent real http connection.
With stub request i could control the returned xml data.