Using Rhino mocks with Windsor

13 views
Skip to first unread message

nmalovic

unread,
Feb 18, 2008, 10:18:21 PM2/18/08
to Rhino.Mocks
Hi,
I am having hard time getting the best way to to test something like
this by using rhino mocks

public class ClassA
{
public void MethodA()
{
IWindsorContainer _container = new WindsorContainer(new
XmlInterpreter());
IDependency _someDependency =
_container.Resolve<IDependency>();

_someDependency.DoSomething();

}
}


The test this I would create mock instance of the IDependency and put
int the test
_container.Kernel.AddComponentInstance<IDependency>(dependency);

but the thing here is that inside of methodA I am reading the config
file.

I don't want to use stubs and alternate configuration files so I am
wondering is there a way to test this method other then dependency
injection of the test container instance to classA so it would replace
container instantiation in MethodA? Injecting the IDependency from
test looks stupid to me too (makes windsor oboslete)

Thanks!





blaz

unread,
Feb 19, 2008, 3:31:35 AM2/19/08
to Rhino.Mocks
Hi,

Best would be if yours code and unit tests are not aware of inversion
of control framework (like Windsor). Ideally Windsor is used only to
integrate your components in finished application.

However if you use Windsor as service provider you should be aware
that Windsor is mainly configuration layer. Actual component container
is MicroKernel. Your code should look something like this:

MockRepository mocks = new MockRepository();
IWindsorContainer _container = new WindsorContainer();
IDependency dependency = mocks.CreateMock<IDependency>();
_container.Kernel.AddComponentInstance("test-dependency",
typeof(IDependency), dependency);
...

IDependency _someDependency = _container.Resolve<IDependency>();
_someDependency.DoSomething();

This way you can initialize dependency container any way you like for
each test individually if necessary.

Ayende Rahien

unread,
Feb 19, 2008, 3:55:36 AM2/19/08
to Rhino...@googlegroups.com
Take a look at the auto mocking container.

nmalovic

unread,
Feb 19, 2008, 5:32:25 AM2/19/08
to Rhino.Mocks
I was aware of MicroKernel usage syntax already but my problem is in
the fact that I don't want to test the _someDependency.DoSomething();
(like you wrote)
I need to test ClassA.MethodA with "injected" mock of _someDependency.

The problem I have is that MethodA has "hardcoded" read out of the
dependency type from config file (real Dependency) and I don't want to
use stubs.

Thanks for your help and time!
Nikola

nmalovic

unread,
Feb 19, 2008, 5:35:23 AM2/19/08
to Rhino.Mocks
I read about AutoMock container but I still have the same problem:
container set up in test is completlly non related from the container
set up in the tested code (loading from config file)

Do you still think I can use auto mock container in my scenario? To me
it looks that if I don't find a way how to replace method container
values with test container values no technology can help me

Thanks!

Nikola


On Feb 19, 3:55 am, "Ayende Rahien" <aye...@ayende.com> wrote:
> Take a look at the auto mocking container.
>

Ayende Rahien

unread,
Feb 19, 2008, 5:54:24 AM2/19/08
to Rhino...@googlegroups.com
Huh? Show us methd A , please

nmalovic

unread,
Feb 19, 2008, 6:02:03 AM2/19/08
to Rhino.Mocks
It is the one from the begining of the post

public class ClassA
{
public void MethodA()
{
IWindsorContainer _container = new WindsorContainer(new
XmlInterpreter());
IDependency _someDependency =
_container.Resolve<IDependency>();
_someDependency.DoSomething();
}
}

(I want to test MethodA with mocking the _someDependency)


On Feb 19, 5:54 am, "Ayende Rahien" <aye...@ayende.com> wrote:
> Huh? Show us methd A , please
>

Ayende Rahien

unread,
Feb 19, 2008, 7:12:40 AM2/19/08
to Rhino...@googlegroups.com
Why on earth are you doing that? This is bad use of the container, you are probaby better with having a singleton container that is used from a static gateway, then you can replace that with AMC

On 2/19/08, nmalovic <nikola....@gmail.com> wrote:

Nikola Malovic

unread,
Feb 19, 2008, 8:13:33 AM2/19/08
to Rhino...@googlegroups.com
I am not using that.. It is just a piece of code I found somewhere on the net googling for Windsor examples

Static gateway has all the sense to me

Thanks,
Nikola
Reply all
Reply to author
Forward
0 new messages