redding / assert

Assertion style testing framework.
https://github.com/redding/assert
MIT License
10 stars 1 forks source link

stubbing class and api #194

Closed kellyredding closed 10 years ago

kellyredding commented 10 years ago

This adds a stub class that can be used to stub method calls. It takes an object and method name and sets itself up on the object. If given a block when created, that block will now be called in place of any method calls. Calling .with on the stub adds args-specific call stubbing. Call .teardown to remove the stub from the object.

Stubbing is noisy - it will complain anytime something seems off (ie you stub a method the object doesn't respond to or add a with stub with mismatched arity).

Stubs are safe to create on the same object/method repeatedly. Stubs are not overly smart - you define with a block and it replaces the call - that's it.

You can create/access/teardown theses stubs using the Assert.stub and Assert.unstub helper methods. These methods track created stubs and allow easy access to them and to tearing them down.

Closes #150.

kellyredding commented 10 years ago

@jcredding ready for review.

jcredding commented 10 years ago

@kellyredding - Once typo comment in your test descriptions. Otherwise this looks good :boom: I like all the coverage of the arity and stubbing scenarios in the tests. Nicely done.

kellyredding commented 10 years ago

@jcredding I've updated this to use a stub key to reference the stubs for lookup. Check that and let me know if you are cool with it: https://github.com/redding/assert/pull/194/files#diff-72227533b1320ec1395b77956a492ffdR11

jcredding commented 10 years ago

@kellyredding - Key stuff looks good :boom:

kellyredding commented 10 years ago

@jcredding updated to do proper arity checking, like we discussed earlier today. Check it: https://github.com/redding/assert/pull/194/files#diff-72227533b1320ec1395b77956a492ffdR79

kellyredding commented 10 years ago

@jcredding I was nervous about performance degradations, so I whipped up a whysoslow bench:

This is benching a trivial stub:

# assert case
obj = '1'
Assert.stub(obj, :to_s){ 'one'}
obj.to_s
Assert.unstub(obj, :to_s)

# mocha case
obj = '1'
obj.stubs(:to_s).returns('one')
obj.to_s
obj.unstub(:to_s)

I ran each 10_000 times, here are the results:

$ bx ruby bench.rb 
Assert: 10000 times
-------------------
whysoslow? ..

mem @ start              24 MB                    ??
mem @ finish             495 MB                   + 470 MB, 1930%

            user        system      total       real
time        1980.0 ms   160.0 ms    2140.0 ms   2142.107 ms

Assert Mocha: 10000 times
-------------------------
whysoslow? ..

mem @ start             284 MB                  ??
mem @ finish            418 MB                  + 134 MB,  47%

            user        system      total       real
time        2790.0 ms   90.0 ms     2880.0 ms   2878.991 ms

Looks like we aren't losing anything performance-wise here. Anyway, FYI.

jcredding commented 10 years ago

@kellyredding - The method arity check looks like what we discussed yesterday. Good stuff. The benchmarks are awesome.