-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up documentation and pack everything in a single file (KISS).
- Loading branch information
Showing
9 changed files
with
138 additions
and
120 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
assert_difference | ||
================= | ||
|
||
A nice assert_difference method similar to the one provided by Rails but with | ||
some improvements. For example: | ||
|
||
assert_difference "Company.count" => +1, "User.count" => +5, "Slot.count" => -1 do | ||
post :something | ||
end | ||
|
||
will assert that a company and 5 users were create (the plus sign is only for | ||
the visual aid) and a slot was removed. | ||
|
||
[Rails' assert_difference](http://api.rubyonrails.org/classes/ActiveSupport/Testing/Assertions.html#method-i-assert_difference) | ||
would require a more verbose syntax: | ||
|
||
assert_difference "Company.count" do | ||
assert_difference "User.count", +5 do | ||
assert_difference "Article.count", -1 do | ||
post :something | ||
end | ||
end | ||
end | ||
|
||
To use it with Test::Unit add this code (untested, please | ||
[let me know](mailto:[email protected]) if it works): | ||
|
||
class Test::Unit::TestCase | ||
include Factory::Syntax::Methods | ||
end | ||
|
||
and to use it with RSpec: | ||
|
||
RSpec.configure do |config| | ||
config.include Factory::Syntax::Methods | ||
end | ||
|
||
For more information read http://pupeno.com/blog/better-assert-difference | ||
|
||
Note on Patches/Pull Requests | ||
----------------------------- | ||
|
||
* Fork the project. | ||
* Make your feature addition or bug fix. | ||
* Add tests for it. This is important so I don't break it in a | ||
future version unintentionally. | ||
* Commit, do not mess with rakefile, version, or history. | ||
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull) | ||
* Send me a pull request. Bonus points for topic branches. | ||
|
||
Copyright | ||
--------- | ||
|
||
Copyright (c) 2010, 2011, José Pablo Fernández. See LICENSE for details. |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,77 @@ | ||
# Copyright © 2011, José Pablo Fernández | ||
# -*- encoding: utf-8 -*- | ||
# Copyright © 2010, 2011, José Pablo Fernández | ||
|
||
require "assert_difference/version" | ||
require "assert_difference/assert_difference" | ||
module AssertDifference | ||
VERSION = "0.3.0" unless defined?(::AssertDifference::VERSION) | ||
|
||
# Test numeric difference between the return value of an expression as a result of what is evaluated | ||
# in the yielded block. | ||
# | ||
# assert_difference "Article.count" do | ||
# post :create, :article => {...} | ||
# end | ||
# | ||
# An arbitrary expression is passed in and evaluated. | ||
# | ||
# assert_difference "assigns(:article).comments(:reload).size" do | ||
# post :create, :comment => {...} | ||
# end | ||
# | ||
# An arbitrary positive or negative difference can be specified. The default is +1. | ||
# | ||
# assert_difference "Article.count", -1 do | ||
# post :delete, :id => ... | ||
# end | ||
# | ||
# An array of expressions can also be passed in and evaluated. | ||
# | ||
# assert_difference [ "Article.count", "Post.count" ], +2 do | ||
# post :create, :article => {...} | ||
# end | ||
# | ||
# A error message can be specified. | ||
# | ||
# assert_difference "Article.count", -1, "An Article should be destroyed" do | ||
# post :delete, :id => ... | ||
# end | ||
# | ||
# Various assertions can be combined into one, instead of writing: | ||
# | ||
# assert_difference "Company.count" do | ||
# assert_difference "User.count", +5 do | ||
# assert_difference "Article.count", -1 do | ||
# post :something | ||
# end | ||
# end | ||
# end | ||
# | ||
# you can *now* write: | ||
# | ||
# assert_difference "Article.count" => 1, "assigns(:article).comments(:reload).size" => 1, "Article.count" => -1 do | ||
# post :something | ||
# end | ||
# | ||
# @param [Array, Hash] expressions array of expressions to evaluate or hash | ||
# table of expressions and expected difference. | ||
# @param [Integer] difference expected difference when using an array or single expression. | ||
# @param [String, nil] message error message to display. One would be constructed if nil. | ||
def assert_difference(expressions, difference = 1, message = nil, &block) | ||
b = block.send(:binding) | ||
if !expressions.is_a? Hash | ||
exps = Array.wrap(expressions) | ||
expressions = {} | ||
exps.each { |e| expressions[e] = difference } | ||
end | ||
|
||
before = {} | ||
expressions.each { |exp, _| before[exp] = eval(exp, b) } | ||
|
||
yield | ||
|
||
expressions.each do |exp, diff| | ||
error = "#{exp.inspect} didn't change by #{diff}" | ||
error = "#{message}.\n#{error}" if message | ||
assert_equal(before[exp] + diff, eval(exp, b), error) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.