Spring for .NET Community Forums    

Go Back   Spring for .NET Community Forums > General > Architecture, Design and Best Practices

Reply
 
Thread Tools Display Modes
  #1  
Old 01-14-2008, 01:15 PM
sideout sideout is offline
Member
Spring User
 
Join Date: Nov 2007
Posts: 71
Default AutoMockingContainer

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?
Reply With Quote
  #2  
Old 01-15-2008, 03:25 PM
Mark Pollack Mark Pollack is offline
Spring.NET Co-Lead
Spring TeamSpring User
 
Join Date: Sep 2004
Location: New York, NY
Posts: 1,603
Default

Hi,
Yup, this is possible, let me work up a prototype and post it back here later today.
Cheers,
Mark
Reply With Quote
  #3  
Old 01-15-2008, 06:32 PM
Mark Pollack Mark Pollack is offline
Spring.NET Co-Lead
Spring TeamSpring User
 
Join Date: Sep 2004
Location: New York, NY
Posts: 1,603
Default

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();
 }
}
protected fields that don't have the attribute can be DI'd from an associated config file. See the docs here, but I need to work on that a little bit more. You can override the method OnSetUp to do initialization work.

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();
 }
}
Let me know how it goes.

Cheers,
Mark
Attached Files
File Type: zip Spring.Testing.NUnit.zip (23.4 KB, 15 views)
File Type: zip Spring.Testing.NUnit.dll.zip (56.6 KB, 9 views)

Last edited by Mark Pollack; 01-15-2008 at 06:40 PM. Reason: code formatting
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 12:10 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.