Re: Mocking behavior between clr types

Ivan,

Seems like a what I was looking for, however according to the
documentation
it says it can only mock virtuals and non-static though.

I don’t want that restriction :frowning: One of the advantages I was hoping to
leverage was the ability to re-write methods that I wouldn’t otherwise
be
able to.

-A

I don’t want that restriction :frowning: One of the advantages I was hoping to
leverage was the ability to re-write methods that I wouldn’t otherwise be
able to.

Unfortunately that’s not a limitation put there by Caricature, but one
put
there by the CLR itself. Unless you want to get into disassembling and
reassembling your dll’s on the fly, you’re out of luck

“disassembling and reassembling your dll’s on the fly” That sounds
like fun… hmmmm, I’ll think that one over a little bit.

On a similar note, you might be interested in my presentation on
Testing ASP.net applications using C#
Testing ASP.net using Ruby – CodeMash session – Ben Hall's Blog

Ben

Hi Ivan,

I was thinking Mono.Cecil first and then the CLR profiling API as I’m
pretty sure that will work. I used a similar approach for
http://blog.benhall.me.uk/2008/11/net-fault-injection-very-early-proof-of.html
but the fault injection was done via some Microsoft Research stuff.
Either approach your re-writing IL, it just depends which layer you
work with.

Lots of interesting problems to solve. Then it would just need a nice
Ruby wrapper so it’s transparent to the end user - ideally
incorporating the backend work into Caricature.

Ben

You don’t need to do anything as drastic as disassemble dlls though :).
There are several solutions and I think the cleanest one is to use the
CLR
profiling API. Another avenue I could take is to use Mono.Cecil but I
don’t
like the idea of the IL rewriting stuff. heck even postsharp would
probably
do the trick.

I need to look into the CLR profiling API to make the rest of the stuff
happening as that is the cleanest solution IMO. The limitation only
exists
if you’re mocking CLR classes for use in other CLR classes. If you’re
going
to use it in ruby code you can do whatever you want.

My mocker does most of what Moq and Rhino.Mocks do, except that it
doesn’t
use LCG or expression trees but just the reflection API once per type.
from
there on out it uses ruby to do its job.

What I need to add but don’t know if they are really necessary are call
count constraints.
And I also want to make the RSpec integration a little bit better so
that
you can enlist an expectation for verification too.

The docs are a bit outdated too, best is to look into
spec/bacon/integration
that’s where I test out the new features or syntax additions.
I have a chapter in my book that explains the use of Caricature with
RSpec
which will be in the update at the end of january. Contact me off list
if
you want a discount :slight_smile:


Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

I’m not sure if the CLR profiling API is in Mono I’d have to find that
out
first.

My thing against mono.cecil is that you need to instrument the DLL and
rewrite the IL before running your app. I’d prefer to do stuff during
instead of beforehand.

I need to be able to intercept and decide whether to go on or not. I’ve
been
googling the CLR profiling API a little yesterday, and I have to go
through
the mailinglist archive to find the links Shri sent me last year about
this
subject. I did get the expert IL assembler book which also came out of
that
discussion but haven’t gotten round to actually reading it yet.

Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

On Tue, Jan 26, 2010 at 9:43 AM, Jimmy S. <

Basically, Ruby is able to do all the crazy mocking stuff because of its
mutable type system, and the only way to make the CLR’s type-system
mutable is to rewrite the IL. To put it in SAT terms:

“rewriting IL” is to “static languages” as “monkey-patching” is to
“dynamic languages”

=P

~Jimmy

TypeMock is doing thing slightly different that allows you to intercept
essentially calls to any member (static, sealed types, non-virtuals,
etc).
It take sa different approach than Rhino and Moq, I’m not sure if
something
similar could be done using Ruby, I’m just dropping the idea here…

yes typemock uses the CLR profiling API and is a paid product. I don’t
know
if typemock runs on Mono either, couldn’t find it on their website.
For me running on mono is one of the base requirements of my mocker,
because
I’m too often on my mac or linux machines to neglect that.

That being said 99% of your tests will not need to mock a static method,
and
if you know about the limitation you can work around it if you’re in
control
of the code.

I’ve looked into this now and I can’t find the ICorProfilerCallback2
interface in Mono on my mac. Mono allows you to write a profiler too
though
but AFAICT that has to be written in C, which I don’t know, and it would
also require to start mono with a different set of parameters.

So I guess the way forward is mono.cecil, but I don’t like the approach
I
need to take at all:

Backup the assemblies in the bin
before running the tests instrument every assembly in the bin folder
with
all the static method calls wrapped inside a before and after hook.
These
hooks call out to caricature and provide the mocking behavior.
Save the modified assembly in the bin

run the test.

if there is an error or an interrupt or exit, restore assemblies from
backup
and this can go wrong easily.

It also means I have to make it a requirement to require caricature
before
requiring any of your application code in the test_helper or spec_helper
file.

If somebody has a better solution by all means share, or better yet put
it
in and send me a pull request


Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

ICorProfilerCallback2 requires C\COM scaryness :slight_smile:

Well this is going to be a very interesting problem to solve when I’m
done
with the edits on my book.

ICorProfilerCallback2 is windows only
Mono.Cecil requires steps before running the app

On the mono IRC channel I’m told that there is no other way to do it in
mono.


Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)

One suggestion would be to prototype with Mono.Cecil to do the IL
rewriting and get rcov working with that. If that does work, then it
might turn out to be an acceptable solution inspite of the need to
instrument the assemblies up front since you don’t do coverage runs too
often.

If the instrumenting is not acceptable at that point, I am sure we can
find samples using ICorProfilerCallback2 that can be used to do the IL
rewriting on the fly.