Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6611 (closed defect: fixed)

Opened 2 years ago

Last modified 1 year ago

[PATCH] add assert_difference

Reported by: brandon Assigned to: topfunky
Priority: normal Milestone: 1.x
Component: ActionPack Version:
Severity: normal Keywords: test assertion
Cc:

Description

There are various versions of the assert_difference assertion floating around out there, and I was surprised when someone pointed out that it wasn't in core yet. The attached patch is a variation of this assertion from http://blog.caboo.se/articles/2006/06/13/a-better-assert_difference. This is a very handy assertion and could be used to clean up a lot of tests, especially the generated tests for scaffold and resource_scaffold.

Old:

def test_should_destroy_account

old_count = Account.count delete :destroy, :id => 1 assert_equal old_count-1, Account.count

assert_redirected_to accounts_path

end

New:

def test_should_destroy_account

assert_difference(Account, :count) { delete :destroy, :id => 1 }

assert_redirected_to accounts_path

end

Attachments

assert_difference.patch (4.7 kB) - added by brandon on 11/13/06 15:42:40.
assert_difference patch
assert_with_proc.patch (6.6 kB) - added by chebuctonian on 01/18/07 13:41:33.

Change History

11/13/06 15:42:40 changed by brandon

  • attachment assert_difference.patch added.

assert_difference patch

(follow-up: ↓ 2 ) 01/09/07 16:15:27 changed by chebuctonian

  • owner changed from David to topfunky.

topfunky, you mentioned working on a method that accepted a Proc. Any progress on that? If you haven't had time, I could give it a try. Examples of how you think this should work would be appreciated.

I've only one qualm, with the method name: it's not as descriptive as assert_increases and assert_decreases.

(in reply to: ↑ 1 ; follow-up: ↓ 4 ) 01/09/07 16:48:15 changed by topfunky

  • owner changed from topfunky to chebuctonian.

The version I currently use is here. Feel free to incorporate it into the patch:

http://topfunky.net/svn/plugins/topfunky_power_tools/lib/topfunky/test_helper.rb

01/18/07 13:41:33 changed by chebuctonian

  • attachment assert_with_proc.patch added.

01/18/07 13:47:34 changed by chebuctonian

  • owner changed from chebuctonian to topfunky.

Added arguments as per your version, as well as the Proc's that you mentioned in the caboose blog. The original tests added by brandon also pass.

Can you review and tell me if it needs changes? In particular, injecting the object in the proc seems a bit magical - is that how you envisioned Proc support?

(in reply to: ↑ 2 ) 02/01/07 00:43:32 changed by chebuctonian

*bump*

Are there any particular concerns with this patch? Anything I can do to make it better?

05/19/07 04:48:03 changed by josh

  • status changed from new to closed.
  • resolution set to fixed.

05/23/07 01:44:27 changed by eostrom

Asserting a difference in the size of an array seems like a common use case, but [6647] doesn't make it easy. This seems intuitive to me:

  assert_difference array, :size, 1 do
    array << addendum
  end

but the use of [objects].flatten makes this an assertion about the size of each member of my array.

The flattening is apparently done so that you can assert the same difference about the same attribute of several objects in one statement, instead of nesting multiple assertions. That doesn't sound to me like a more common case than array size, but I'm a newbie, so I could be wrong.

Still, what about:

  objects = [objects] unless Array === objects

That way you can pass in a single object (User) or multiple objects ([User, Group]), and I can check my array appender by wrapping the array ([array]).