Collection of useful helpers for writing unit/functional/integration testunit tests.
This uses method_missing to provide additional functions for each of your
This asserts that the lists are the same size and have the same items. Any
difference is reported.
fixtures :candy_bars assert_list_of_candy_bars [:almond_joys, :m_and_ms], CandyBar.find_nutty()
This asserts that the lists are the same size and have the same items, but order
assert_set_of_candy_bars [:almond_joys, :m_and_ms], CandyBar.find_nutty()
This asserts that all the items are found in the second parameter, but order
assert_contains_candy_bars [:almond_joys, :m_and_ms], CandyBar.find_nutty()
Pre and Post Condition Checks
assert_changes 'a' do a = 'world' end
assert a != 'world' a = 'world' assert_equal 'world', a
'a' passed to assert changes is evaluated in the block context, both before and after the block is run. The first eval is call the pre-condition, and the second the post-condition.
Be explicit about a state change by specifying both the starting and ending values using an expression pointing to array of before and after values:
o.answer = 'yes' assert_changes 'o.answer' => ['yes','no'] do o.answer = 'no' end
When given one value, it is considered the post-condition value.
The precondition is that the value does NOT equal it:
i = true assert_changes 'i' => false do # read as: i changes to false i = false end
Assert that several things change by passing an array:
a,b = 'hello','hi' assert_changes ['a','b'] do a = 'world' b = 'earth' end
Or use a hash, and pass multiple pre/post conditions of arbitrary complexity:
assert_changes 'post(:a).status' => [:preview, :published], 'comment(:c).status' => [:preview, :deleted] do ... end
assert_no_changes has an extended parameter possibilities:
i,j = 'hello','hi' assert_no_changes 'i' do ... # i (before) == i (after) assert_no_changes 'i'=>'hello' do ... # i == 'hello' before and after assert_no_changes ['i','j'] do ... # neither i nor j change assert_no_changes 'i'=>'hello','j'=>'hi' do # or be explicit with multiple
A Complex Example
Provide multiple assertions of arbitrary complexity, referencing fixtures, etc.
Note that assert_changes supports the :no_change symbol:
assert_changes 'inotech.services.public.include?(categories(:a))' => [true, :no_change], 'inotech.services.public.include?(categories(:b))'=>false, 'inotech.services.public.include?(categories(:c))'=>false do post :edit_services_dialog, :id=>inotech.id, :service_category_id=>categories(:a).id end
- Installing the gem:
sudo gem install ndp-ayudante --source=http://gems.github.com
- or in environment.rb
Rails::Initializer.run do |config| config.gem "ndp-ayudante", :source => "http://gems.github.com", :lib=>'ayudante' end
- Better error messages on fixture helpers
- Add assert_includes_N fixture helper
- Add assert_excludes_N fixture helper
(The MIT License)
Copyright © 2009 Andrew J. Peterson, email@example.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
‘Software’), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.