![]() |
|
#1
|
|||
|
|||
|
I've been evaluating Spring and Castle Windsor for IoC and other needs. I'm finding a lot of useful features in Spring, however, one thing that seems extremely helpful in Windsor is the AutoMockingContainer
http://blog.eleutian.com/2007/08/05/...Container.aspx http://www.ayende.com/Blog/archive/2...Container.aspx I can't seem to find anything comparable for Spring.Net. Does something like this exist? Is it at least possible? |
|
#2
|
|||
|
|||
|
Hi,
Yup, this is possible, let me work up a prototype and post it back here later today. Cheers, Mark |
|
#3
|
|||
|
|||
|
Hi,
I've created a POC of this feature. The source code is attached as well the compiled dll. You should be able to drop this into the current Spring distribution (it is for .NET 2.0). Let me know if you have trouble. The basic features are along the lines of what you linked to, though I did not yet implement different mocking strategies. I did however, take advantage of the support for performing dependency injection on NUnit tests, this lets you write the automock test like this. Code:
[TestFixture]
public class SimpleServiceTestUsingInjection : AbstractMockContainerTests
{
[MockDependencies]
protected SimpleService simpleService;
public SimpleServiceTestUsingInjection()
{
PopulateProtectedVariables = true;
}
[Test]
public void DoWork()
{
using(Mocks.Unordered())
{
GetObject<IService1>().DoWork();
GetObject<IService2>().DoWork();
GetObject<IService3>().DoWork();
}
Mocks.ReplayAll();
simpleService.DoWork();
Mocks.VerifyAll();
}
}
You could be a bit more explicit in the usage and nothing here is specific to NUnit. Code:
[TestFixture]
public class SimpleServiceTests
{
private MockRepository mocks;
private AutoMockingObjectFactory objectFactory;
private SimpleService simpleService;
[SetUp]
public void Setup()
{
mocks = new MockRepository();
objectFactory = new AutoMockingObjectFactory(mocks);
objectFactory.RegisterByType<SimpleService>();
simpleService = objectFactory.GetObject<SimpleService>();
}
[Test]
public void DoWork()
{
using (mocks.Unordered())
{
objectFactory.GetObject<IService1>().DoWork();
objectFactory.GetObject<IService2>().DoWork();
objectFactory.GetObject<IService3>().DoWork();
}
mocks.ReplayAll();
simpleService.DoWork();
mocks.VerifyAll();
}
}
Cheers, Mark Last edited by Mark Pollack; 01-15-2008 at 06:40 PM. Reason: code formatting |
![]() |
| Thread Tools | |
| Display Modes | |
|
|